Soham Dasgupta
Soham Dasgupta

Reputation: 5199

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?

Upvotes: 0

Views: 991

Answers (1)

Casperah
Casperah

Reputation: 4564

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.

Upvotes: 1

Related Questions