[Java] 纯文本查看 复制代码import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regu {
public static void main(String[] args) {
//匹配6位顺增
String pattern = "(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}\\d";
Pattern pa = Pattern.compile(pattern);
String mc = "123456";
Matcher ma = pa.matcher(mc);
System.out.println("6位顺增 :" + ma.matches());
System.out.println("*******分割线*******");
//匹配6位顺降
pattern = "(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5}\\d";
pa = Pattern.compile(pattern);
mc = "654321";
ma = pa.matcher(mc);
System.out.println("6位顺降 :" + ma.matches());
System.out.println("*******分割线*******");
//匹配6位顺增或顺降
pattern = "(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5})\\d";
pa = Pattern.compile(pattern);
mc = "234567";
ma = pa.matcher(mc);
System.out.println("6位顺增或顺降 :" + ma.matches());
System.out.println("*******分割线*******");
//匹配4-9位连续的数字
pattern = "(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3,}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3,})\\d";
pa = Pattern.compile(pattern);
mc = "123456789";
ma = pa.matcher(mc);
System.out.println("4-9位连续的数字 :" + ma.matches());
System.out.println("*******分割线*******");
//匹配3位以上的重复数字
pattern = "([\\d])\\1{2,}";
pa = Pattern.compile(pattern);
mc = "888888";
ma = pa.matcher(mc);
System.out.println("3位以上的重复数字 :" + ma.matches());
System.out.println("*******分割线*******");
//匹配手机号码类
pattern = "(13[0-9]|15[0-9]|18[0-9])([\\d]{2,4}){2}";
pa = Pattern.compile(pattern);
mc = "18930333033";
ma = pa.matcher(mc);
System.out.println("手机号码 :" + ma.matches());
System.out.println("*******分割线*******");
// //匹配连同号如“112233”“222333”
// pattern = "([\\d])\1{1,}([\\d])\\2{2,}"; //这个测试没有通过,要重新编写
// pa = Pattern.compile(pattern);
// mc = "33111";
// ma = pa.matcher(mc);
// System.out.println("连同号如112233 , 222333 :" + ma.matches());
System.out.println("*******分割线*******");
//匹配2233类型
pattern = "([\\d])\\1{1,}([\\d])\\2{1,}";
pa = Pattern.compile(pattern);
mc = "2233";
ma = pa.matcher(mc);
System.out.println("2233类型 :" + ma.matches());
System.out.println("*******分割线*******");
}
}