tags:

views:

35

answers:

1

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 and 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.

+6  A: 

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".

Mark Byers