ITeye博客

检查不包含重复字符的正则表达式

Goro · 2012-08-15 16:31 · Java · 编程语言 · Markdown

以下可以根据需要修改。


//匹配不包含重复字母
String regex = "^[a-z]*([a-z])[a-z]*\\1[a-z]*$";
System.out.println(!"ABCDEF".matches(regex));
System.out.println(!"ABCDEA".matches(regex));
System.out.println(!"A".matches(regex));

//匹配不包含连续重复字母
regex = "^[a-z]*([a-z])\\1[a-z]*$";
System.out.println(!"ABCDEF".matches(regex));
System.out.println(!"ABCDEA".matches(regex));
System.out.println(!"ABBCDDEF".matches(regex));
System.out.println(!"A".matches(regex));

输出


true
false
true
true
true
false
true

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。