验证手机号
public class Demo3 {
public static void main(String[] args) {
//校验手机号码, 邮箱,电话号码
checkPhone();
System.out.println("程序结束");
}
public static void checkPhone(){
Scanner sc = new Scanner(System.in);
do{
System.out.println("请输入你的手机号码");
String number = sc.next();
if(number.matches("1[3-9]\\d{9}")){
System.out.println("输入正确");
break;
}else{
System.out.println("输入有误请重新输入");
}
}while(true);
}
=====================
public static void checkEmail(){
Scanner sc = new Scanner(System.in);
do{
System.out.println("请输入你注册的邮箱");
String number = sc.next();
//判断邮箱格式是否正确 123456789@qq.com
//判断邮箱格式是否正确 1234567dfg89s@163.com
//判断邮箱格式是否正确 123456789@pci.com.cn
if(number.matches("\\w{1,30}@[a-zA-Z0-9]{2,20}\\.([a-zA-Z0-9]{2,20}){1,2}")){
System.out.println("输入正确");
break;
}else{
System.out.println("输入有误请重新输入");
}
}while(true);
}
====================
一些方法
public class RegexDemo04 {
public static void main(String[] args) {
String names = "小路134sad蓉儿215sad过儿";
String[] arrs = names.split("\\w+");//分割
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}//小路 , 蓉儿. 过儿
String name2 = names.replaceAll("\\w+"," ");//将//w+换成空格
System.out.println(name2);
}
}
===========================
爬虫基础
String rs = "来自黑马程序学习Java,电话020-43422424,或者联系邮箱" +
"itcast@itcast.cn, 电话15730397976,020323322" +
"邮箱bozai@qq.cn,400-100-3233, 4001003232";
//定义爬取规则
String regex ="\\w{1,30}@[a-zA-Z0-9]{2,20}\\.([a-zA-Z0-9]{2,20}){1,2}|" +
"1[3-9]\\d{9}|" +
"(0\\d{2,6}-?\\d{5,20})|" +
"(400-?\\d{3,9}-?\\d{3,9})";
//2. 把这个爬取规则编译成匹配对象
Pattern pattern = Pattern.compile(regex);
//3.得到一个内容匹配器对象
Matcher matcher = pattern.matcher(rs);
//4.开始找了
while (matcher.find()){
String rs1 = matcher.group();
System.out.println(rs1);
}