开发者

simple Regex question / multiple matches

Newbie Regex question / C#:

Consider (.*)=(.*) and how it would match "A = B = C"

I exepected to get two match objects back since there are two ways to group and match:

(A = B) = (C)   

     or 

(A) = (B = C)

However I get back only one match object (the first case). So I guess I don't understand why the match collection is a collection - since I can't seem to get more than one item into it. Can someone explain ?


fyi - for the above test I just used the immed window:

?Reg开发者_开发问答ex.Matches("A = B = C", "(.*)=(.*)").Count
 1

?Regex.Matches("A = B = C", "(.*)=(.*)")[0].Groups[1].Captures[0]
 Value: "A = B"

?Regex.Matches("A = B = C", "(.*)=(.*)")[0].Groups[1].Captures[1]
 Value: "C"


The collection returned by Matches contains consecutive matches, not alternative matches for the same section of the string. So if you pass in a string like "A = B\nC = D", you'll get back two matches: one for "A = B" and one for "C = D" (as . does not match line breaks).


The .* is normally greedy. This means it matches as many characters as possible whilst satisfying the expression. Therefore the first .* matches the "A = B", leaving the "C" for the second .*

You can change the behaviour using a ? after the expression. .*? will match as few characters as possible whilst satisfying the expression.


Regular expressions work in such a way that when a match is found, the part of the input that has been matched already is ignored from then on. So when A = B has been matched, that part of the input will not be subject to any more matching.


It doesn't work that way....

If your input text was:

"A = B = C\r\nW = X = Z"

and your expression was

"([^=]?) = ([^=]?) = ([^=]?)"

then you would get multiple results. Please read documentation! :-D


The * quantifier is greedy. It makes the first expression .* matches many chars as possible, so, the expression will always match (A = B) = (C).

Using the not greedy quantifier *? will match (A) = (B = C).

Try using it!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜