Given string:
String str = "STACK 2013 OVERFLOW3";
And pattern:
Pattern pattern = Pattern.compile("\\b\\w+\\s\\b");
The output is:
STACK
2013
Why? I read that once a character is used in the match, it can't be used again in next match.
But here we have first match for "\\b\\w+\\s\\b"
:
\b used for boundary (before word STACK)
\w+ used for STACK word
\s used for space after STACK
\b used for boundary (before word 2013)
This results, as expected, in match "STACK ".
And then we have second match for "\b\w+\s\b":
\b used for boundary (before word 2013) <--- HERE this boundary is used second time
\w+ used for 2013 word
\s used for space after 2013
\b used for boundary (before word OVERFLOW3)
Why word boundary before word "2013" is used twice in these matchings?
Full code to reproduce:
public static void main(String[] args) {
String str = "STACK 2013 OVERFLOW3";
Pattern pattern = Pattern.compile("\\b\\w+\\s\\b");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}