Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greedy, Non-Greedy, All-Greedy Matching in C# Regex

Tags:

How can I get all the matches in the following example:

// Only "abcd" is matched MatchCollection greedyMatches = Regex.Matches("abcd", @"ab.*");  // Only "ab" is matched MatchCollection lazyMatches   = Regex.Matches("abcd", @"ab.*?");  // How can I get all matches: "ab", "abc", "abcd" 

P.S.: I want to have the all matches in a generic manner. The example above is just an example.

like image 898
Peter Lee Avatar asked Oct 09 '10 22:10

Peter Lee


People also ask

What is greedy match and non-greedy match?

It means the greedy quantifiers will match their preceding elements as much as possible to return to the biggest match possible. On the other hand, the non-greedy quantifiers will match as little as possible to return the smallest match possible. non-greedy quantifiers are the opposite of greedy ones.

What is the difference between lazy matching and greedy matching in regular expressions?

'Greedy' means match longest possible string. 'Lazy' means match shortest possible string.

What is greedy matching?

A greedy matching is one in which the maxi- mum possible number of applicants are matched to their first choice post, and subject to that condition, the maximum possible number are matched to their second choice post, and so on.

What does regex greedy mean?

From the basic lesson you might encounter the following characters: . * is greedy, meaning that it will ignore the next delimiter of your regex until it itself is not fulfilled, unless the regex following . * is against the end of the target string.


1 Answers

You could use something like:

MatchCollection nonGreedyMatches = Regex.Matches("abcd", @"(((ab)c)d)"); 

Then you should have three backreferences with ab, abc and abcd.

But, to be honest, this kind of regex doesn't makes too much sense, especially when it gets bigger it becomes unreadable.

Edit:

MatchCollection nonGreedyMatches = Regex.Matches("abcd", @"ab.?"); 

And you got an error there btw. This can only match ab and abc (read: ab + any (optional) character

Lazy version of:

MatchCollection greedyMatches    = Regex.Matches("abcd", @"ab.*"); 

is:

MatchCollection nonGreedyMatches    = Regex.Matches("abcd", @"ab.*?"); 
like image 148
Tseng Avatar answered Mar 22 '23 07:03

Tseng