最近在做手機号校驗,在此記錄一下,希望幫助更多的人。
public static final String REGEX_MOBILE = "^[1][3578][0-9]{9}$";
1、手機号開頭集合
166,
176,177,178
180,181,182,183,184,185,186,187,188,189
145,147
130,131,132,133,134,135,136,137,138,139
150,151,152,153,155,156,157,158,159
198,199
2、正規表達式
public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
String regExp = "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(str);
return m.matches();
}
包含的手機号整理有
13開頭的後面跟0-9的任意8位數;
15開頭的後面跟除了4以外的0-9的任意8位數;
18開頭的後面跟0-9的任意8位數;
17開頭的後面跟0-8的任意8位數,或者17[^9];
147,145開頭後面跟任意8位數;
166開頭的後面跟任意8位數;
198,199開頭後面跟任意8位數;