简体   繁体   中英

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:

?Regex.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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM