Regular expression capture group returning wrong result for slightly different text

109 Views Asked by At

I'm trying to write a PCRE with a capture group that looks for numerical values within two strings.

Case A:

There is/are 1 thread(s) in total that are monitored by this Valve and may be stuck.

Case B:

There is/are still 10 thread(s) that are monitored by this Valve and may be stuck.

I've written a regex that when run on case A returns "1", but for case B it returns the wrong result "still". How can I write one that will return "1" for A and "10" for B?

Here's what I have:

There\s+is.are\s+(?<threadNumber>[^\s]+)]
1

There are 1 best solutions below

0
On

You may add an optional (?:\S+\s+)? before the number part to match an optional "word" and only use \d+ to match digits:

There\s+is.are\s+(?:\S+\s+)?(?<threadNumber>\d+)
                 ^^^^^^^^^^^                ^^^

See the regex demo

Details

  • There - a literal substring
  • \s+ - 1 or more whitespaces
  • is - a literal substring
  • . - any char but a line break char
  • are - a literal substring
  • \s+ - 1 or more whitespaces
  • (?:\S+\s+)? - an optional sequence of:
    • \S+ - 1+ non-whitespace chars
    • \s+ - 1 or more whitespaces
  • (?<threadNumber>\d+) - 1 or more digits (Group "threadNumber").