用正则表达式验证联系电话(及区号)

一、格式一

翻了几篇博客,说下面的正则表达式是验证手机号和固话的

/^(13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9})$|(^(0\d{10})|^(0\d{2}-\d{8}))$/

自己便拿来验证了下出现了一个小问题,手机号都没问题,11位数,而测试固话时出现了问题,固话因为区号的原因,会出现“—”符号的位置不一样,比如有的区号只有三位,而有的区号有四位数,导致后

^(0\d{2}-\d{8})

有问题,写 2的话,验证4位数的区号不通过;写 3的话,验证3位数的区号不通过。

二、格式二

验证手机,电话的正则表达式(11位手机号,4位区号,3位区号,7位或者8位手机号码)...

电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号) 

((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)

三、格式三

Java使用正则表达式验证手机号和电话号码

中国电信号段 133、149、153、173、177、180、181、189、199
中国联通号段 130、131、132、145、155、156、166、175、176、185、186
中国移动号段 134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188、198
其他号段
14号段以前为上网卡专属号段,如中国联通的是145,中国移动的是147等等。
虚拟运营商
电信:1700、1701、1702
移动:1703、1705、1706
联通:1704、1707、1708、1709、171
卫星通信:1349

 

package com.qf.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName ZhznegZe
 * @Description TODO
 * @Author WYD
 * @Date 2022/8/16 9:04
 * @Version 1.0
 */
public class ZhznegZe {

    /**
     * 手机号验证
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isMobile(final String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
//        String s = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }
    /**
     * 电话号码验证
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isPhone(final String str) {
        Pattern p1 = null, p2 = null;
        Matcher m = null;
        boolean b = false;
        p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
        if (str.length() > 9) {
            m = p1.matcher(str);
            b = m.matches();
        } else {
            m = p2.matcher(str);
            b = m.matches();
        }
        return b;
    }

    public static void main(String[] args) {
        String phone = "15135765275";
        String phone2 = "0214-88889999";
        String phone3 = "8888999";
        String phone4 = "151357652758";
        //测试1
        if(isPhone(phone) || isMobile(phone)){
            System.out.println("1这是符合的");
        }
        //测试2
        if(isPhone(phone2) || isMobile(phone2)){
            System.out.println("2这是符合的");
        }
        //测试3
        if(isPhone(phone3) || isMobile(phone3)){
            System.out.println("3这是符合的");
        }
        //测试4
        if(isPhone(phone4) || isMobile(phone4)){
            System.out.println("4这是符合的");
        }else{
            System.out.println("不符合");
        }
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彦登的登

动力来源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值