我已经经历过:正则表达式使用 Java 模式匹配字符串中的四个重复字母 https://stackoverflow.com/questions/2622776/regex-to-match-4-repeated-letters-in-string-using-java-pattern
and
正则表达式匹配任何重复超过 10 次的字符 https://stackoverflow.com/questions/1660694/regular-expression-to-match-any-character-being-repeated-more-than-10-times
但它们对我来说没有用。如果我只想检查字符串是否包含重复字符(如 1111、abccccd、12aaaa3b 等),它们就很好。我想要的是检查字符串是否完全由重复字符组成,即 aabb111、1111222、11222aaa 等。
谁能帮我解决这个问题吗?
Use ((.)\2+)+
作为模式:
String pattern = "((.)\\2+)+";
System.out.println("a".matches(pattern)); // false
System.out.println("1aaa".matches(pattern)); // false
System.out.println("aa".matches(pattern)); // true
System.out.println("aabb111".matches(pattern)); // true
System.out.println("1111222".matches(pattern)); // true
System.out.println("11222aaa".matches(pattern)); // true
System.out.println("etc.".matches(pattern)); // false
关于图案:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)