I need to write a regex, that would identify a word that have a repeating character set at the end. According to the following code fragment, the repeating character set is An
. I need to write a regex so this will be spotted and displayed.
According to the following code, \\w
will match any word character (including digit, letter, or special character). But i only want to identify english characters.
String stringToMatch = "IranAnAn";
Pattern p = Pattern.compile("(\\w)\\1+");
Matcher m = p.matcher(stringToMatch);
if (m.find())
{
System.out.println("Word contains duplicate characters " + m.group(1));
}
UPDATE
Word contains duplicate characters a
Word contains duplicate characters a
Word contains duplicate characters An