Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\s+ not matching all whitespaces?

Tags:

java

regex

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!

like image 946
David Avatar asked Nov 11 '12 11:11

David


3 Answers

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+)
like image 181
Sergey Kalinichenko Avatar answered Sep 24 '22 02:09

Sergey Kalinichenko


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.

like image 43
slackwing Avatar answered Sep 24 '22 02:09

slackwing


Because first parsing outputs to " 18 " and remaining string is "19 Text" which is not a match.

like image 20
Pankaj Avatar answered Sep 26 '22 02:09

Pankaj