在下面的正则表达式中
^(?:(?!(\w)\1\b).)*$
不匹配最后两个字符相同的字符串。例如1233
问题是什么?
答案 0 :(得分:1)
如果您想(不)匹配相同的字符,try the following code:
public static void main (String[] args)
{
final String SAME_CHAR = "(.)\\1*";
final List<String> list1 = Arrays.asList("Apple","Aa", "A1a", "AAg");
final List<String> list2 = Arrays.asList("AAA", "111", "aaaaa", "a");
for (String str: list1) {
System.out.println(str + ": " + !str.matches(SAME_CHAR));
}
for (String str: list2) {
System.out.println(str + ": " + !str.matches(SAME_CHAR));
}
}
结果输出为:
Apple: true
Aa: true
A1a: true
AAg: true
AAA: false
111: false
aaaaa: false
a: false