7

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.

JCCyC
  • 16,140
  • 11
  • 48
  • 75

1 Answers1

23

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
  • 811,555
  • 193
  • 1,581
  • 1,452