一、正则表达式
1.定义:用于定义字符串的内容
2.用途:(1)匹配单个,多个字符串(matches)
(2)简化代码,让代码变得更简洁,更优雅
(3)校验信息,包括电话,座机,邮箱
(4)切割字符串,替换字符串
(5)爬取信息中的内容(可以强记忆,死代码)(pattern)
详细介绍:
(1)匹配单个,多个字符串(matches)
package Regex;
/**
* 目标:深入学习正则表达式
*/
public class test1 {
public static void main(String[] args) {
//只能是 a b c
System.out.println("a".matches("[abc]"));//true
System.out.println("z".matches("[abc]"));//false
//不能出现 a b c
System.out.println("a".matches("[^abc]"));//false
System.out.println("z".matches("[^abc]"));//true
System.out.println("a".matches("\\d"));//false
System.out.println("3".matches("\\d"));//true
System.out.println("333".matches("\\d"));//false
System.out.println("z".matches("\\w"));//true
System.out.println("2".matches("\\w"));//true
System.out.println("21".matches("\\w"));//false
System.out.println("你".matches("\\w"));//false
System.out.println("你".matches("\\W"));//true
System.out.println("----------------------------------");
//以上正则表达式只能匹配单个字符
//校验密码
//必须是数字,字母,下划线 至少6位
System.out.println("33ds3c".matches("\\w{6,}"));//true
System.out.println("33dsc".matches("\\w{6,}"));//false
System.out.println("33dssc234你".matches("\\w{6,}"));//false
//验证码必须是数字和字符 必须是4位
System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));//true
System.out.println("23_F".matches("[a-zA-Z0-9]{4}"));//false
System.out.println("23dF".matches("[\\w&&[^_]]{4}"));//true
System.out.println("23_F".matches("[\\w&&[^_]]{4}"));//false
}
}
(2)简化代码,让代码变得更简洁,更优雅
package Regex;
public class RegexDemo {
public static void main(String[] args) {
//需求:校验QQ号码,必须全部数字6--20位
System.out.println(checkQQ("324535"));//true
System.out.println(checkQQ(null));//false
System.out.println(checkQQ("32453a"));//false
//
System.out.println(checkQQ1("324535"));//true
}
//使用正则表达式表示
public static boolean checkQQ1(String qq){
return qq != null && qq.matches("\\d{6,20}");
}
//常规表示
public static boolean checkQQ(String qq){
//1.判断QQ号的长度是否满足要求
if(qq == null || qq.length() < 6 || qq.length() > 20){
return false;
}
//2.判断QQ中是否每个都是数字,不是返回false
for (int i = 0; i < qq.length(); i++) {
//获取每个字符
char ch = qq.charAt(i);
//判断这个字符是不是数字,不是返回false
if(ch < 0 || ch > '9'){
return false;
}
}return true;//肯定合法了
}
}
(3)校验信息,包括电话,座机,邮箱
package Regex;
import java.util.Scanner;
public class Example1 {
public static void main(String[] args) {
//目标:校验 手机号码 邮箱 电话号码
//checkPhone();
//checkEmail();
checkTel();
}
public static void checkTel(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册电话(座机)号码:");
String tel = sc.next();
//判断手机号码的格式是否正确
if(tel.matches("0\\d{2,6}-?\\d{5,20}")){
System.out.println("格式正确,注册完成!");
break;
}else{
System.out.println("格式有误!");
}
}
}
public static void checkEmail(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册邮箱:");
String email = sc.next();
//判断手机号码的格式是否正确
if(email.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("格式有误!");
}
}
}
public static void checkPhone(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册手机号码:");
String phone = sc.next();
//判断手机号码的格式是否正确
if(phone.matches("1[3-9]\\d{9}")){
System.out.println("手机号码格式正确,注册完成!");
break;
}else{
System.out.println("格式有误!");
}
}
}
}
(4)切割字符串,替换字符串
package Regex;
/**
* 目标:正则表达式在方法中的应用
* public String[] split(String regex)
* ----按照正则表达式匹配的内容进行分割字符串,返回一个字符串数组
* public String replaceAll(String regex,String newStr)
* ----按照正则表达式匹配的内容进行替换
*/
public class SplitDemo {
public static void main(String[] args) {
String names ="小路hdetretd蓉儿reteryyhgdf果儿";
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);//按照正则表达式匹配的内容进行替换(替换为空格) //小路 蓉儿 果儿
}
}
爬取信息中的内容(可以强记忆,死代码)(pattern)
package Regex;
/**
* 利用正则表达式爬取信息中的内容(需要记住!!!)
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class matchDemo {
public static void main(String[] args) {
String rs = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" +
"itcast@itcast.cn,电话182205213457,0203232323" +
"邮箱bozai@itcast.cn,400-100-3233,4001003232";
//需求:从上面的内容中爬取出 电话号码和邮箱
//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})"
+ "|(0\\d{2,6}-?\\d{5,20})|(400-?\\d{3,9})";
//2.把这个爬取规则编译成匹配对象-------------把规则翻译(compile)成匹配对象
Pattern pattern = Pattern.compile(regex);
//3.得到一个内容匹配对象---------------调matcher方法 到rs那去匹配(找内容中的规则)
// 让匹配器拿着匹配规则到rs里面去爬取
Matcher matcher = pattern.matcher(rs);
//4.开始找了
while(matcher.find()){//找到了就返回true
String rs1 = matcher.group();//每找到一个就取一组
System.out.println(rs1);
}
}
}
3.匹配规则