I'm trying to match this string:
Text 18 19 Text
With this regex:
\s+\d\d\s+
The string has two digits, each of them are surrounded by a leading and a trailing space.
So I'm thinking - this should give me 18 and 19 right? It doesn't, it only gives me only 18.
I'm testing with this tester here: http://java-regex-tester.appspot.com/
Thanks!
The reason that you do not match the second item is that the space between 18
and 19
is consumed by the trailing \s+
of the first match. You should make a non-consuming zero-width regexp for the trailing blank, for example by using the lookahead syntax or a token for zero-width boundary:
\s+\d\d(?=\s+)
Use this instead:
\b\d\d\b
Your regex isn't matching the second number because the first match has already "eaten up" all the spaces.
Meanwhile, \b
is a "word boundary," and what is known as a zero-width (meta-)character: it doesn't "eat up" anything while it matches.
Because first parsing outputs to " 18 " and remaining string is "19 Text" which is not a match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With