Iterating through MatchCollection
-
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?
-
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?
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]