import java.util.regex.Pattern; import java.util.regex.Matcher; class RepeatedCharacterCheck { public static void main (String[] args) throws java.lang.Exception { String stringToMatch = "abccdeeefenonn"; Pattern p = Pattern.compile("(\\w)\\1+"); Matcher m = p.matcher(stringToMatch); while (m.find()) { System.out.println("Duplicate character " + m.group(0)); } } }
Output
Duplicate character ccDuplicate character eee
Duplicate character nn