package com.baidu.oct15;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// 手机号码验证
public class RegexDemo_1
{
public static void main(String[] args)
{
String tel = "";
Scanner in = new Scanner(System.in);
while(!tel.equals("over") )
{
System.out.println("请输入需要验证的手机号码:");
tel = in.nextLine();
if(tel.equals("over"))
{System.out.println("退出验证");
break;
}
if(isAtelNumber(tel))
System.out.println("正确:该号码是一个手机号码");
else
System.out.println("错误:该号码不是手机号码");
};
}
public static boolean isAtelNumber(String tel)
{
String regex = "1[358]\d{9}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(tel);
return m.matches();
}
}
运行效果: