regex - Pattern and Matcher in Java: Matcher only finds one match instead of two -
this question has answer here:
- overlapping matches in regex 3 answers
i'm working pattern , matcher in java. have following code:
string searchstring = "0,00,0"; string searchintext = "0,00,00,0" pattern p = pattern.compile(searchstring); matcher m = p.matcher(searchstring); while(m.find){ ... }
my problem matcher finds 1 match first 0 4th zero. there should match 3rd 0 last zero.
can me? there workaround?
getting overlapping matches regex tricky, if you're not familiar regexes.
if you're not using regex functionality (like in example), indexof(string, int)
, keep increasing index you're doing search.
int index = 0; while((index = text.indexof(pattern, index)) > -1) { system.out.println(index + " " + pattern); index++; }
Comments
Post a Comment