语法:
package com.hb2.正则表达式;
public class Demo1 {
public static void main(String[] args) {
//帮助文档搜pattern
System.out.println("a".matches("[abc]"));
System.out.println("z".matches("[abc]"));
System.out.println("a".matches("[^abc]"));
System.out.println("z".matches("[^abc]"));
System.out.println("a".matches("\\d"));
System.out.println("1".matches("\\d"));
System.out.println("333".matches("\\d"));
System.out.println("z".matches("\\w"));
System.out.println("2".matches("\\w"));
System.out.println("21".matches("\\w"));
System.out.println("你".matches("\\w"));
System.out.println();
//校验密码
//必须是数字、字母、下划线、至少6位
System.out.println("1234jladf".matches("\\w{6,}"));
System.out.println("12".matches("\\w{6,}"));
System.out.println();
//校验码 必须是数字和字符,必须是4位
System.out.println("aasd".matches("[a-zA-Z0-9]{4}"));
System.out.println("a_sd".matches("[a-zA-Z0-9]{4}"));
System.out.println("adsd".matches("[\\w&&[^_]]{4}"));
System.out.println("a_sd".matches("[\\w&&[^_]]{4}"));
}
}
package com.hb2.正则表达式;
public class Demo1_QQAccountLength {
public static void main(String[] args) {
String qq = "12341231";
System.out.println(checkqq(qq));
}
public static boolean checkqq(String qq){
return qq!=null && qq.matches("\\d{6,20}");//QQ号码为6到20位的数字,返回true
}
}
package com.hb2.正则表达式;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
//检验邮箱,手机号码
// checkPhone();
checkEmail();
}
//电话号码: tel.matches("0\\d{2,6}-?\\d{2,20}")
//例:027-3572457 0273572457
public static void checkPhone(){
Scanner sc= new Scanner(System.in);
String phone = sc.next();
//判断手机号码格式是否正确
if(phone.matches("1[3-9]\\d{9}")){
System.out.println("手机号码正确,注册完成!");
}else {
System.out.println("手机格式错误");
}
}
public static void checkEmail(){
Scanner sc= new Scanner(System.in);
String Email = sc.next();
//判断邮箱
// 1316565465@qq.com
// 131656546df5@163.com
// 131656546egqg5@pci.com.cn
if(Email.matches("\\w{1,30}@[a-zA-z0-9]{2,20}(\\.[a-zA-z0-9]{2,20}){1,2}")){
System.out.println("邮箱正确,注册完成!");
}else{
System.out.println("邮箱格式错误");
}
}
}
在方法中的使用:
package com.hb2.正则表达式;
public class 在方法中的应用 {
/*
public String[] split(String regex)
--按正则表达式匹配的内容进行分割字符串,返回一个字符串数组
public String replaceAll(String regex, String newStr)
--按照正则表达式匹配的内容进行替换
*/
public static void main(String[] args) {
String names = "萨芬afafadfasd鞍山adsfasddf等方";
String [] arrs = names.split("\\w+");
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}
String names2 = names.replaceAll("\\w+"," ");
System.out.println(names2);
}
}
爬取信息:
package com.hb2.正则表达式;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class 爬邮箱电话号码 {
public static void main(String[] args) {
String rs ="卢卡斯九点零分,电话020-1234132" +
"邮箱加1234@qq.com,电话18704703301,12345678999" +
"邮箱lkajf@163.com,8208208820,4001002323";
//1、定义爬取规则
String regex ="(\\w{1,30}@[a-zA-z0-9]{2,20}(\\.[a-zA-z0-9]{2,20}){1,2})" +
"|(1[3-9]\\d{9})|(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);
}
}
}