How to search for "minimum match" in C #?

Let's say I have a multiline string like this:

STARTFRUIT banana ENDFRUIT STARTFRUIT avocado ENDFRUIT STARTVEGGIE rhubarb ENDVEGGIE STARTFRUIT lime ENDFRUIT 

I want to look for all the fruits, without vegetables. 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 that instead of returning an array of three matches to me, it gives me a big match, including the first STARTFRUIT and the beginning and last ENDFRUIT at the end. Is there a way to "minimize" a coincidence search? I do not see any help in RegexOptions .

+6
c # regex
source share
1 answer

After the quantifier, use the inanimate modifier (question mark):

  "STARTFRUIT. *? ENDFRUIT"
              ^
          add this

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

+22
source share

All Articles