正则表达式匹配模式后的句子中的单词

时间:2013-08-24 19:45:47

标签: c# .net regex

我想在句子fish egg chicken beef中捕捉群组How much is fish egg chicken beef ?。我试过

how much is ((?>\w+))* \\?

但它只返回fish作为第二组。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

也许正则表达式应该是:

How much is (.*)\?

或者如果你想匹配每个捕获中的所有单词,但只有一个单词:

How much is (?:(\w+)\s*)+\?

Regex regexWords = new Regex(@"How much is (?:(\w+)\s*)+\?");

foreach(Capture word in regexWords.Match(input).Groups[1].Captures)
{
    // word.Value contains one word.
}

祝你好运。