RegEx.MatchCollection.CopyTo method fails!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mortimer Schnurd

    RegEx.MatchCollection.CopyTo method fails!

    Has anyone had any luck getting this CopyTo method to work?

    I can iterate through a MatchCollection and move each Match.Value to
    the System.Array without a problem. I just can't figure out why the
    CopyTo method will not work.

    Example:
    // Find all the Tables in an html string (page)
    MatchCollection mc = Regex.Matches(h tml, "<table.+</table");
    string[] t = new string[mc.Count];
    mc.CopyTo(t, 0);

    ERROR HERE IS:
    An unhandled exception of type 'System.Invalid CastException' occurred
    in mscorlib.dll

    Additional information: At least one element in the source array could
    not be cast down to the destination array type.

    -*-*-*-*-*
    In my particular case, mc.Count = 4. If I use a for each loop to
    iterate through and set each element of the array, there is no problem
    whatsoever.

    TIA for any information that may help me to understand why the CopyTo
    did not work.

    --
    John Wood a.k.a. Mortimer Schnurd
  • Brian Davis

    #2
    Re: RegEx.MatchColl ection.CopyTo method fails!


    The array t should be an array of Match objects, not strings.

    Brian Davis




    "Mortimer Schnurd" <fugetaboutit@h otsmail.com> wrote in message
    news:s391d0p6s1 5qtpb7kvad881uf 6lda2u1vm@4ax.c om...[color=blue]
    > Has anyone had any luck getting this CopyTo method to work?
    >
    > I can iterate through a MatchCollection and move each Match.Value to
    > the System.Array without a problem. I just can't figure out why the
    > CopyTo method will not work.
    >
    > Example:
    > // Find all the Tables in an html string (page)
    > MatchCollection mc = Regex.Matches(h tml, "<table.+</table");
    > string[] t = new string[mc.Count];
    > mc.CopyTo(t, 0);
    >
    > ERROR HERE IS:
    > An unhandled exception of type 'System.Invalid CastException' occurred
    > in mscorlib.dll
    >
    > Additional information: At least one element in the source array could
    > not be cast down to the destination array type.
    >
    > -*-*-*-*-*
    > In my particular case, mc.Count = 4. If I use a for each loop to
    > iterate through and set each element of the array, there is no problem
    > whatsoever.
    >
    > TIA for any information that may help me to understand why the CopyTo
    > did not work.
    >
    > --
    > John Wood a.k.a. Mortimer Schnurd[/color]


    Comment

    • Mortimer Schnurd

      #3
      Re: RegEx.MatchColl ection.CopyTo method fails!

      On Wed, 16 Jun 2004 16:31:49 -0400, in msg
      <#cY#FB#UEHA.40 88@TK2MSFTNGP09 .phx.gbl>, "Brian Davis" <@> wrote:
      [color=blue]
      >
      >The array t should be an array of Match objects, not strings.
      >
      >Brian Davis
      >http://www.knowdotnet.com
      >[/color]
      That was it!

      Thanks for the help.

      MatchCollection mc = Regex.Matches(h tml, "<table.+</table");
      Match[] t = new Match[mc.Count];
      mc.CopyTo(t,0);

      --
      JW

      Comment

      Working...