简体   繁体   中英

Regex to match words in sentence after a pattern

I want to capture the group fish egg chicken beef in the sentence How much is fish egg chicken beef ? . I tried with

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

But its only returning fish as the second group. What am I doing wrong here?

Maybe the regex should be:

How much is (.*)\?

Or if you want to match all the words but one word in each capture:

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

Good luck with your quest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM