group() in java.util.regex.Matcher? -
this question has answer here:
- java regex capturing groups 4 answers
string pattern = "(.*)(\\d+)"; while(m.find()){ system.out.println(m.group(1)); system.out.println(m.group(2)); } input:
this order placed qt3000! ok? output :
this order placed qt300 0 why in first line of output doesnot include third 0 i.e qt3000? why second line of output doesnot print 3000?
this because of .* greedy , leave 1 \d group \\d+.make non greedy .*? , want.
.* match upto last char , start backtracking until finds \d.so when encouters 0 3000 while coming backwards stops right there.
.*? stop @ first instance of \d i.e 3.so 3000 in second group.
Comments
Post a Comment