- importjava.util.regex.Matcher;
- importjava.util.regex.Pattern;
-
- publicclassRegu{
- staticvoidmain(String[]args){
-
- Stringpattern="(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}\\d";
- Patternpa=Pattern.compile(pattern);
- Stringmc="123456";
- Matcherma=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());
- //匹配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";
- mc="234567";
- System.out.println("6位顺增或顺降:"+ma.matches());
- //匹配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";
- mc="123456789";
- System.out.println("4-9位连续的数字:"+ma.matches());
- //匹配3位以上的重复数字
- pattern="([\\d])\\1{2,}";
- mc="888888";
- System.out.println("3位以上的重复数字:"+ma.matches());
- //匹配手机号码类
- pattern="(13[0-9]|15[0-9]|18[0-9])([\\d]{2,4}){2}";
- mc="18930333033";
- System.out.println("手机号码:"+ma.matches());
- ////匹配连同号如“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());
- //匹配2233类型
- pattern="([\\d])\\1{1,}([\\d])\\2{1,250);line-height:18px;"> mc="2233";
- System.out.println("2233类型:"+ma.matches());
- }
- }
原文链接:https://www.f2er.com/regex/357680.html