通过正则判断手机用户

写的~! 留下脚印
配置文件config.propertise

unicom=133|132|131|134
chinamobile=135|136|137|138|139|159
reg_templete=^0?(_phone_)[0-9]{8}

主要的类方法

package demo.phone;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Pattern;

public class Phone {
private String unicom;
private String chinamobile;
private String reg_templete;
private String runicom;
private String rchinamobile;
public Phone() {
//读取配置文件
Properties prop = new Properties();
try {
prop.load(new FileInputStream("D://workspace//gsj//phone//src//demo//phone//config.properties"));
//联通的号段
unicom=prop.getProperty("unicom");
//移动的号段
chinamobile=prop.getProperty("chinamobile");
//表达式模板
reg_templete=prop.getProperty("reg_templete");
//联通的表达式
runicom=reg_templete.replaceAll("_phone_",unicom);
//移动的表达式
rchinamobile=reg_templete.replaceAll("_phone_",chinamobile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public int validate(String phone){
if(validateUnicom(phone))
return 1;//联通的号码
if(validateChinamobile(phone))
return 2;//移动的号码
return 0;//小灵通号码
}
//判断是否是联通的号码
private boolean validateUnicom(String phone){
return Pattern.compile(runicom).matcher(phone).matches();
}
//判断是否是移动的号码
private boolean validateChinamobile(String phone){
return Pattern.compile(rchinamobile).matcher(phone).matches();
}
}

用于测试的方法

package demo.phone;

public class Main {

public static void main(String[] args) {
//如果是1表示联通,2移动,3小灵通
Say("013578481235");
Say("13678481235");
Say("13378481235");
Say("13178481235");
Say("015978481235");
Say("99690814");
Say("89892015");
}
public static void Say(String phone){
Phone p = new Phone();
System.out.println(phone+" "+p.validate(phone));
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值