java正则表达式校验电话号码

五迷三道 提交于 2020-03-02 11:45:32
 /**
     * 验证电话号码
     */
    private static Pattern pattern = Pattern.compile("0\\d{2,3}[-]?\\d{7,8}|0\\d{2,3}\\s?\\d{7,8}|[1-9]\\d{6,7}");



/**
     * 校验电话号码格式,校验异常返回空
     * 
     * @param officePhone
     *            办公电话
     * @return 校验结果
     */
    private static String validOfficePhonePattern(String officePhone) {
        if (StringUtils.isBlank(officePhone)) {
            return officePhone;
        } else {
            String newStr = StringUtils.replace(officePhone, "--", "-").replaceAll("——", "-");
            Matcher matcher = pattern.matcher(newStr);
            boolean bool = matcher.matches();
            if (bool) {
                return newStr;
            } else {
                return StringUtils.EMPTY;
            }
        }
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!