1. ([0-9])\1{5} 或 ([\d])\1{5} 連續相同的6位數字 如:333333java
([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表明位置,從左到右遞增spa
import java.util.regex.*;code
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
}
}ci
2.判斷字符串是不是連續字母或者是連續的數字能夠用截取字符串而後比較ascii碼的值判斷連續的次數字符串
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; }