How can I find repeated characters with a regex in Java?

Try "(\\w)\\1+"

The \\w matches any word character (letter, digit, or underscore) and the \\1+ matches whatever was in the first set of parentheses, one or more times. So you wind up matching any occurrence of a word character, followed immediately by one or more of the same word character again.

(Note that I gave the regex as a Java string, i.e. with the backslashes already doubled for you)

Leave a Comment