匹配手机号
当前所有手机号段
中国电信来号段
133、149、153、173、177、180、181、189、199
中国联通号段
130、131、132、145、155、156、166、171、175、176、185、186、166
中国移动号段
134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、172、178、182、183、184、187、188、198
其他号段
14号段以前为上网卡专属号源段,如中国联通的是145,中国移动的是147等等。
虚拟运营商
电信:1700、1701、1702
移动:zhidao1703、1705、1706
联通:1704、1707、1708、1709、171
卫星通信:1349
知道手机号段就很好处理了
String regex = “^((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(166)|(17([0-3]|[5-8]))|(18[0-9])|(19[89]))\d{8}$”;
测试:Pattern pattern = Pattern.compile(regex);
Matcher ma = pattern.matcher(“13312345678”);
System.out.println(ma.matches());
匹配邮箱
邮箱规则
1.用户名:字母数字下划线组成
2.@符号
3.网址:字母数字组成
4.小数点.
5.组织域名:2-4位字母组成不区分大小写
6.第4条和第五条可重复两次如.com.cn或者.org.cn
String regex = “^\w{1,}@[0-9a-zA-Z]{1,}(\.[a-zA-Z]{2,3}){1,2}$”;
测试: Pattern p =Pattern.compile(regex );
Matcher m = p.matcher("_132131zz@study.com.cn");
System.out.println(m.matches());
其它常用正则表达式
匹配中文字符 | [\u4e00-\u9fa5] |
---|---|
匹配空白行 | \n\s*\r |
匹配HTML标记 | <(\S*?)[^>]>.?</\1|<.*?/> |
匹配首尾空白字符 | ^\s*|\s*$ |
匹配Email地址 | \w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)* |
匹配网址URL | [a-zA-Z]+://[^\s]* |
匹配国内电话号码 | \d{3}-\d{8} |
匹配QQ号 | [1-9][0-9]{4,} |
匹配中国邮政编码 | [1-9]\d{5}(?!\d) |
匹配身份证 | (\d{14}[\dX])|(\d{17}[\dX]) |
匹配ip地址 | \d+.\d+.\d+.\d+ |