如何在C#中进行“最小匹配”正则表达式搜索?

时间:2022-09-13 08:44:18

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.

问题是,它给了我一个包含第一个STARTFRUIT和开头以及最后一个ENDFRUIT的大匹配,而不是返回三个匹配的数组。有没有办法“最小化”匹配搜索?我在RegexOptions中看不到任何帮助。

1 个解决方案

#1


22  

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

请注意,这里的问号与用作量词时的含义不同,它表示“匹配零或一”。

#1


22  

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

请注意,这里的问号与用作量词时的含义不同,它表示“匹配零或一”。