如何在 Java 中使用正则表达式查找重复字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/664194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find repeated characters with a regex in Java?
提问by JediPotPie
Can anyone give me a Java regex to identify repeated characters in a string? I am only looking for characters that are repeated immediately and they can be letters or digits.
谁能给我一个 Java 正则表达式来识别字符串中的重复字符?我只寻找立即重复的字符,它们可以是字母或数字。
Example:
例子:
abccde <- looking for this (immediately repeating c's)
abcdce <- not this (c's seperated by another character)
abccde <- 寻找这个(立即重复 c)
abcdce <- 不是这个(c 由另一个字符分隔)
采纳答案by David Z
Try "(\\w)\\1+"
尝试 "(\\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.
在\\w
任何单词字符(字母,数字或下划线)和匹配\\1+
无论是在第一组括号,一次或多次的比赛。因此,您最终会匹配任何出现的单词字符,然后立即再次匹配一个或多个相同的单词字符。
(Note that I gave the regex as a Java string, i.e. with the backslashes already doubled for you)
(请注意,我将正则表达式作为 Java 字符串提供,即反斜杠已经为您加倍了)
回答by David Z
Regular Expressions are expensive. You would probably be better off just storing the last character and checking to see if the next one is the same. Something along the lines of:
正则表达式很昂贵。您最好只存储最后一个字符并检查下一个字符是否相同。类似的东西:
String s;
char c1, c2;
c1 = s.charAt(0);
for(int i=1;i<s.length(); i++){
char c2 = s.charAt(i);
// Check if they are equal here
c1=c2;
}
回答by Simon Nickerson
String stringToMatch = "abccdef";
Pattern p = Pattern.compile("(\w)\1+");
Matcher m = p.matcher(stringToMatch);
if (m.find())
{
System.out.println("Duplicate character " + m.group(1));
}