([0-9a-zA-Z])\1{5} 连续相同的6位数字或字母 如:222222 cccccc ZZZZZZ
([\d])\1{2}([a-z])\2{2} 连续相同3位数字后根连续相同的三位小写字母 如:222www
([\d])\1{2}([a-z])\2{2}|([a-z])\3{2}([\d])\4{2} 同上,但是能匹配数字+字母或字母+数字 如:222www 或 www222
这么多的例子自己可以扩展,要注意的就是 \1 \2代表位置,从左到右递增
import java.util.regex.*;
public class demo {
public static void main (String[] args) {
Pattern pattern = Pattern.compile("([\\d])\\1{5}");
Matcher matcher = pattern.matcher("666666");
System.out.println(matcher.matches()); //true
}
}
public static void main(String[] args) {
String str = "ab1234567ab";
boolean ll = hasLH(str, 5);
System.out.println(ll);
}
private static boolean hasLH(String str,int count) {
int pre = 0;
int len = 1;
for (int i = 0; i < str.length(); i++) {
String s = str.substring(i, i + 1);
char c = s.charAt(0);
if (i == 0) {
pre = c;
}
if (c - 1 == pre) {
len++;
if(len>=count){
return true;
}
}else {
len = 1;
}
pre = c;
}
return false;
}
https://www.leftso.com/article/507.html