Your problem is that the indices returned by match.start()
correspond to the position of the character as it appeared in the original string when you matched it; however, as you rewrite the string c
every time, these indices become incorrect.
The best approach to solve this is to use replaceAll
, for example:
System.out.println(c.replaceAll("[^a-zA-Z0-9]", ""));