group() in java.util.regex.Matcher? -


this question has answer here:

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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -