开发者

Why doesnt Regex.Match have a GetEnumerator function?

开发者 https://www.devze.com 2022-12-12 01:03 出处:网络
Regex.Match has a .Success and .NextMatch why doesnt it have a GetEnumerator function? With my logic it seems easy enough to implement. But it is开发者_如何学JAVAnt in 3.5 so can anyone tell me why

Regex.Match has a .Success and .NextMatch why doesnt it have a GetEnumerator function?

With my logic it seems easy enough to implement. But it is开发者_如何学JAVAnt in 3.5 so can anyone tell me why not?

foreach (var m in Regex.Match("dummy text", "mm")) error CS1579: foreach statement cannot operate on variables of type 'System.Text.RegularExpressions.Match' because 'System.Text.RegularExpressions.Match' does not contain a public definition for 'GetEnumerator'


Perhaps you want Regex.Matches?


Regex.Match 

returns the first instance of the pattern that is matched in the string.

You probably want

Regex.Matches

, which returns a MatchCollection of all the matches in the string.

MSDN Article on Regex.Match


Because the Match object is immutable (and NextMatch() does not change the context of the current match, but gives you a reference to the next one, which is different from IEnumerable.MoveNext() ).

But you can do this:

for (Match m=Regex.Match("dummy text", "mm"); m.Success; m=m.NextMatch()) {
    // loop code
}
0

精彩评论

暂无评论...
验证码 换一张
取 消