Using PCRE, I want to capture only and all digits in a line which follows a line in which a certain string appears. Say the string is "STRING99". Example:
car string99 house 45b
22 dog 1 cat
women 6 man
In this case, the desired result is:
221
As asked a similar question some time ago, however, back then trying to capture the numbers in the SAME line where the string appears ( Regex (PCRE): Match all digits conditional upon presence of a string ). While the question is similar, I don't think the answer, if there is one at all, will be similar. The approach using the newline anchor ^ does not work in this case.
I am looking for a single regular expression without any other programming code. It would be easy to accomplish with two consecutive regex operations, but this not what I'm looking for.
Maybe you could try:
(?:\bstring99\b.*?\n|\G(?!^))[^\d\n]*\K\d
See the online demo
(?:
- Open non-capture group:
\bstring99\b
- Literally match "string99" between word-boundaries..*?\n
- Lazy match up to (including) nearest newline character.|
- Or:\G(?!^)
- Asserts position at the end of the previous match but prevent it to be the start of the string for the first match using a negative lookahead.)
- Close non-capture group.[^\d\n]*
- Match 0+ non-digit/newline characters.\K
- Resets the starting point of the reported match.\d
- Match a digit.