13

我需要编写一个正则表达式,它将识别一个在结尾处具有重复字符集的单词。根据以下代码片段,重复字符集为. 我需要编写一个正则表达式,以便发现并显示它。An

根据以下代码,\\w将匹配任何单词字符(包括数字、字母或特殊字符)。但我只想识别英文字符。

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));
}

更新

Word contains duplicate characters a
Word contains duplicate characters a
Word contains duplicate characters An
4

2 回答 2

9

You want to catch as many characters in your set as possible, so instead of (\\w) you should use (\\w+) and you want the sequence to be at the end, so you need to add $ (and I have removed the + after \\1 which is not useful to detect repetition: only one repetition is needed):

Pattern p = Pattern.compile("(\\w+)\\1$");

Your program then outputs An as expected.

Finally, if you only want to capture ascii characters, you can use [a-zA-Z] instead of \\w:

Pattern p = Pattern.compile("([a-zA-Z]+)\\1$");

And if you want the character set to be at least 2 characters:

Pattern p = Pattern.compile("([a-zA-Z]{2,})\\1$");
于 2013-07-22T17:45:52.037 回答
1

如果“仅英文字符”是指 AZ 和 az,则以下正则表达式将起作用:

".*([A-Za-z]{2,})\\1$"
于 2013-07-22T17:51:11.523 回答