public void name() throws Exception {
Pattern p = Pattern.compile("\\d{1,2}?");
String input = "09";
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
while(m.find()) {
System.out.println("match = "+m.group());
}
}
Output of the above method is :
match = 0
match = 9
Now, I am just adding parenthesis to the regexp:
public void name() throws Exception {
Pattern p = Pattern.compile("(\\d{1,2})?");
String input = "09";
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
while(m.find()) {
System.out.println("match = "+m.group());
}
}
And the output becomes:
match = 09
match =
- Why do parenthesis make the matching greedy here?
- [Edit,added later]Why is empty string not matched in the first case?
In order for a quantifier (
?
,*
,+
or{n,m}
) to be reluctant (non-greedy or lazy), it must by followed by a?
. Therefore, the pattern\\d{1,2}?
is reluctant.On the other hand,
(\\d{1,2})?
is composed of two levels of greedy quantifiers:(\\d{1,2})
containing a pattern with a greedy quantifier{1,2}
,?
greedy quantifier.Therefore,
(\\d{1,2})?
is greedy because the?
does not immediately follow a quantifier (there is a parenthesis in between that closes the group), hence it does not act as the metacharacter for a reluctant regex.See this page about quantifiers as a reference.