开发者

Need C# RegEx to find a a word and a number.

开发者 https://www.devze.com 2023-03-24 06:02 出处:网络
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 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消