Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a "minimal match" Regex search in C#? [duplicate]

Tags:

c#

regex

Let's say I have a multi-line string like this:

STARTFRUIT
banana
ENDFRUIT

STARTFRUIT
avocado
ENDFRUIT

STARTVEGGIE
rhubarb
ENDVEGGIE

STARTFRUIT
lime
ENDFRUIT

I want to search for all fruit, no veggies. I try this:

MatchCollection myMatches = Regex.Matches(tbBlob.Text, "STARTFRUIT.*ENDFRUIT", RegexOptions.Singleline);

foreach (var myMatch in myMatches)
{
    Forms.MessageBox.Show(String.Format("Match: {0}", myMatch), "Match", Forms.MessageBoxButtons.OK, Forms.MessageBoxIcon.Information);
}

The problem is, instead of returning me an array of three matches, it gives me a big match encompassing the first STARTFRUIT at the beginning and the last ENDFRUIT at the end. Is there a way to "minimalize" the match search? I don't see any help in RegexOptions.

like image 497
JCCyC Avatar asked Oct 28 '10 20:10

JCCyC


People also ask

What is a lazy regex?

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

How do you stop greedy in regex?

You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" .

Does C# have regex?

In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex. The . Net Framework provides a regular expression engine that allows the pattern matching.

What is question mark in regex?

A regular expression followed by a question mark (?) matches zero or one occurrences of the regular expression. Two regular expressions concatenated match an occurrence of the first followed by an occurrence of the second.


1 Answers

Use a non-greedy modifier (a question mark) after the quantifier:

"STARTFRUIT.*?ENDFRUIT"
             ^
         add this

Note that the question-mark here has a different meaning here than when it is used as a quantifier, where it means "match zero or one".

like image 81
Mark Byers Avatar answered Oct 19 '22 23:10

Mark Byers