Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Iterating through MatchCollection

Iterating through MatchCollection

Scheduled Pinned Locked Moved C#
questioncomregexhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uglyeyes
    wrote on last edited by
    #1

    Hi, I have a MatchCollection1 which holds 1,2 and 3 MatchCollection2 holds A, B and C now I want to print in below order, 1 A 2 B 3 C how can i do that. thank you i think i need to do something like <pre> StreamWriter sw = new StreamWriter(@"file.txt"); foreach (Match match in MatchCollection1 ) { sw.writeline(match); //TODO get first item of collection in matchcollection2 and delete the first item.          </pre> I also cant find Item property as stated in this link http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.item.aspx[^] i dont know the todo part please help?

    L 1 Reply Last reply
    0
    • U uglyeyes

      Hi, I have a MatchCollection1 which holds 1,2 and 3 MatchCollection2 holds A, B and C now I want to print in below order, 1 A 2 B 3 C how can i do that. thank you i think i need to do something like <pre> StreamWriter sw = new StreamWriter(@"file.txt"); foreach (Match match in MatchCollection1 ) { sw.writeline(match); //TODO get first item of collection in matchcollection2 and delete the first item.          </pre> I also cant find Item property as stated in this link http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.item.aspx[^] i dont know the todo part please help?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      It is a bit confusing, what the MSDN page is saying (see its example) is that MatchCollection has an indexer (not really a property), i.e. you can use it as if it were an array; and that solves your problem entirely:

      for (int i=0; i<MatchCollection1.Count; i++) {
      Console.WriteLine(matchCollection1[i].Value);
      Console.WriteLine(matchCollection2[i].Value);
      }

      which will fail if MatchCollection1 holds more entries than MatchCollection2. FYI: They use similar words here[^]. Conclusion: it pays to look at the examples! :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      [The QA section does it automatically now, I hope we soon get it on regular forums as well]


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups