1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 正则判断手机号地区_判断手机号正则表达式

正则判断手机号地区_判断手机号正则表达式

时间:2022-05-27 02:28:21

相关推荐

正则判断手机号地区_判断手机号正则表达式

在开发中有时会有这样的需求,就是判断用户输入的手机号码是否正确,这里就需要使用到了正则表达式。

这里贴出一个电话号码检查的工具类,基本的电话号码格式都能够满足了。

**[java]** [view plain](/afei__/article/details/51482801#) [copy](/afei__/article/details/51482801#)

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import java.util.regex.PatternSyntaxException;

public class PhoneFormatCheckUtils {

/**

* 大陆号码或香港号码均可

*/

public static boolean isPhoneLegal(String str)throws PatternSyntaxException {

return isChinaPhoneLegal(str) || isHKPhoneLegal(str);

}

/**

* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数

* 此方法中前三位格式有:

* 13+任意数

* 15+除4的任意数

* 18+除1和4的任意数

* 17+除9的任意数

* 147

*/

public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {

String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";

Pattern p = pile(regExp);

Matcher m = p.matcher(str);

return m.matches();

}

/**

* 香港手机号码8位数,5|6|8|9开头+7位任意数

*/

public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {

String regExp = "^(5|6|8|9)\\d{7}$";

Pattern p = pile(regExp);

Matcher m = p.matcher(str);

return m.matches();

}

}

下面是移动电话 手机号 和邮编的判断

package com.tmall.epp.web.module.util;

import java.util.regex.Pattern;

/**

* 移动电话、固话、邮编的校验

* @since .12.15

*/

public class ImportCsvValidate {

/**

* 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))

* @param mobile 移动、联通、电信运营商的号码段

*

移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)

*、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)

*

联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)

*

电信的号段:133、153、180(未启用)、189

* @return 验证成功返回true,验证失败返回false

*/

public static boolean isMobile(String mobile){

String regex = "(\\+\\d+)?1[3458]\\d{9}$";

return Pattern.matches(regex, mobile);

}

/**

* 区号+座机号码+分机号码

* @param fixedPhone

* @return

*/

public static boolean isFixedPhone(String fixedPhone){

String reg="(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +

"(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";

return Pattern.matches(reg, fixedPhone);

}

/**

* 匹配中国邮政编码

* @param postcode 邮政编码

* @return 验证成功返回true,验证失败返回false

*/

public static boolean isPostCode(String postCode){

String reg = "[1-9]\\d{5}";

return Pattern.matches(reg, postCode);

}

public static void main(String[] args) {

String mobile = "18600000000";

boolean ret = isMobile(mobile);

System.out.println(ret);

String postCode = "110200";

ret = isPostCode(postCode);

System.out.println(ret);

String isFixedPhone = "0571-8888880-111";

ret = isFixedPhone(isFixedPhone);

System.out.println(ret);

}

}

当然,这里使用正则表达式不一定都面面俱到了,以后万一又有什么新的格式了也不好说,不过道理都是一样的,修改一下正则表达式的规则就行。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。