-1

I'm working with Pattern and Matcher in Java. I have the 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 is that the Matcher only finds one match from the first zero to the 4th zero. But there should be another match from the 3rd zero to the last zero.

Can someone help me? Is there a workaround?

Generic Bot
  • 309
  • 1
  • 4
  • 8
Jannik
  • 90
  • 10

1 Answers1

2

Getting overlapping matches with regex is tricky, especially if you're not very familiar with regexes.

If you're not really using regex functionality (like in your example), you could easily do this with an indexOf(String, int) and keep increasing the index from which you're doing the search.

int index = 0;
while((index = text.indexOf(pattern, index)) > -1) {
    System.out.println(index + " " + pattern);
    index++;
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Shouldn't the `+ 1` be inside the parentheses, like `index = text.indexOf(pattern, index + 1);` – SamWhan Aug 18 '17 at 08:42
  • No, we're trying to increase the `index`. Then the while loop can see if there are any patterns left. – Kayaman Aug 18 '17 at 08:43
  • No, we don't need to start from zero. We can to start from the first occurrence of the pattern (which may be zero). Well, it doesn't really make a difference. I guess avoiding an extra call is better. – Kayaman Aug 18 '17 at 08:45
  • Well, IMO, it should then be `int index = text.indexOf(pattern, 0); while( index > -1) { ... index = text.indexOf(pattern, index + 1); }` – SamWhan Aug 18 '17 at 08:48
  • You can write this in many ways, so there's really no *should*. – Kayaman Aug 18 '17 at 08:49
  • Let's not. This isn't a problem interesting enough to spend more time on. – Kayaman Aug 18 '17 at 08:51
  • OK. I'll let it go now, but... Having the '+ 1' outside the parentheses is still is wrong. In the second iteration index will be as in the first, only +1 which isn't a correct match. This make the code faulty. – SamWhan Aug 18 '17 at 08:53
  • No, it's not faulty. Try it out and you'll see. The whole idea is that the index is being increased, so `+1` is there on purpose. If you claim my code is faulty, I challenge you to provide a runnable test case where it's wrong. Otherwise be quiet. – Kayaman Aug 18 '17 at 08:57
  • There you go - http://ideone.com/YWpbr4. – SamWhan Aug 18 '17 at 09:03
  • Ah you're right, I wasn't checking the indexes, I'd write it in a different way though, see edit. – Kayaman Aug 18 '17 at 09:07