0
点赞
收藏
分享

微信扫一扫

JavaWeb - 正则表达式之手机号校验(移动、联通、电信)


import com.chinadaas.platform.dspserviceexecutor.executor.ApiWork
import com.chinadaas.platform.dspserviceexecutor.script.IConditionScript
import com.chinadaas.platform.servicecenter.common.constant.RespCode
import com.chinadaas.platform.servicecenter.common.util.ResultUtil
import com.chinadaas.platform.servicecenter.common.vo.RxGraphVO
import com.google.common.collect.Maps
import org.jeasy.flows.work.WorkContext
import org.jeasy.flows.work.WorkReport
import org.jeasy.flows.work.WorkStatus
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException

class CheckPhone {

// 中国联通号码格式验证 手机段:130,131,132,155,156,185,186,145,176,1709
private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}\$)|(^1709\\d{7}\$)"

// 中国移动号码格式验证 手机段:134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
private static final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}\$)|(^1705\\d{7}\$)"

// 中国电信号码格式验证 手机段: 133,153,180,181,189,177,1700
private static final String CHINA_TELECOM_PATTERN = "(^1(33|53|77|8[019])\\d{8}\$)|(^1700\\d{7}\$)"

/**
* 验证【联通】手机号码的格式
* @param str
* @return boolean
*/
private static boolean isChinaUnicomPhoneNum(String str) {
if (!isChinaPhoneLegal(str)) {
return false
}
return str == null || str.trim() == "" ? false : match(CHINA_UNICOM_PATTERN, str)
}

/**
* 验证【移动】手机号码的格式
* @param str
* @return boolean
*/
private static boolean isChinaMobilePhoneNum(String str) {
if (!isChinaPhoneLegal(str)) {
return false
}
return str == null || str.trim() == "" ? false : match(CHINA_MOBILE_PATTERN, str)
}

/**
* 验证【电信】手机号码的格式
* @param str
* @return boolean
*/
private static boolean isChinaTelecomPhoneNum(String str) {
if (!isChinaPhoneLegal(str)) {
return false
}
return str == null || str.trim() == "" ? false : match(CHINA_TELECOM_PATTERN, str)
}

/**
* 验证手机号格式
* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
* 此方法中前三位格式有:
* 13+任意数
* 15+除4的任意数
* 18+除1和4的任意数
* 17+除9的任意数
* 147
*/
private 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 = Pattern.compile(regExp)
Matcher m = p.matcher(str)
return m.matches()
}

/**
* 香港手机号码8位数,5|6|8|9开头+7位任意数
*/
private static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
String regExp = "^(5|6|8|9)\\d{7}\$"
Pattern p = Pattern.compile(regExp)
Matcher m = p.matcher(str)
return m.matches()
}

/**
* 执行正则表达式
* @param pat
* @param str
* @return boolean
*/
private static boolean match(String pat, String str) {
Pattern pattern = Pattern.compile(pat)
Matcher match = pattern.matcher(str)
return match.find()
}

static void main(String[] args) {
String phone = "15555221100"
System.out.println(isChinaUnicomPhoneNum(phone))
}
}
  • 以上是 Groovy 代码,跟 Java 其实很像,这里注意的地方是正则表达式中的“$”,在 Java 中不需要前面带“\”,但是在 Groovy代码中需要带“\”,因为是保留字。


举报

相关推荐

0 条评论