I have a need to find a word and a开发者_如何学Python number in a sentence like this:
The state Mchine is at stage 4. next stage is queued.
I need to find only state and number 4. and ignore the rest.
I have this:
@"\b(state)(?:\W+\w+){1,10}?\W+(\d)*\b"
but it is also including all the words in between the matches.
What am I doing wrong?
Try this:
var input = "The state Mchine is at stage 4. next stage is queued.";
var pattern = @"[^\s]+.(?<state>\w+)[\w\s]+(?<stage>\d+)\.";
var match = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
Console.WriteLine(match.Groups["state"].Value); //state
Console.WriteLine(match.Groups["stage"].Value); //4
Console.ReadLine();
精彩评论