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]+)]
You may add an optional
(?:\S+\s+)?
before the number part to match an optional "word" and only use\d+
to match digits:See the regex demo
Details
There
- a literal substring\s+
- 1 or more whitespacesis
- a literal substring.
- any char but a line break charare
- 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").