Your problem is that the indexes returned by match.start() correspond to the character position, as it was in the original line when you matched it; however, when you rewrite the string c each time, these indices become invalid.
The best approach to solve this problem is to use replaceAll , for example:
System.out.println(c.replaceAll("[^a-zA-Z0-9]", ""));
Sebastien le callonnec
source share