目录
什么是正则表达式?
public class RegexDemo {
public static void main(String[] args) {
String qq = "2233445566";
boolean check = check(qq);
System.out.println(check);
}
//校验的方法
public static boolean check(String qq) {
int len = qq.length();
if (len >= 6 && len <= 20) {
if (!qq.startsWith("0")) {
for (int i = 0; i < qq.length(); i++) {
if (!(qq.charAt(i) >= '0' && qq.charAt(i) <= '9')) {
return false;
}
}
return true;
}
}
return false;
}
}
明显代码很繁琐,这时我们就可以使用正则表达式来简化代码
String qq = "2233445566";
System.out.println(qq.matches("[1-9]\\d{5,19}"));
// [1-9] 表示必须以1-9的数字开头
// \\d{5,19}表示任意数字至少出现5次,至多出现19次
APi 查找规则位置:
1. 正则表达式---字符类(一个大括号匹配一个字符):
- 代码理解:
//只能是a b c其中之一
System.out.println("-----------1-------------");
System.out.println("a".matches("[abc]")); // true
System.out.println("z".matches("[abc]")); // false
//不能出现a b c
System.out.println("-----------2-------------");
System.out.println("a".matches("[^abc]")); // false
System.out.println("z".matches("[^abc]")); // true
System.out.println("zz".matches("[^abc]")); //false
System.out.println("zz".matches("[^abc][^abc]")); //true
// a到z或A到Z(包括头尾的范围)
System.out.println("-----------3-------------");
System.out.println("a".matches("[a-zA-Z]")); // true
System.out.println("z".matches("[a-zA-Z]")); // true
System.out.println("aa".matches("[a-zA-Z]"));//false
System.out.println("zz".matches("[a-zA-Z]")); //false
System.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); //true
System.out.println("0".matches("[a-zA-Z]"));//false
System.out.println("0".matches("[a-zA-Z0-9]"));//true
// [a-d[m-p]] a到d,或m到p 和上面一个意思
System.out.println("-----------4-------------");
System.out.println("a".matches("[a-d[m-p]]"));//true
System.out.println("d".matches("[a-d[m-p]]")); //true
System.out.println("m".matches("[a-d[m-p]]")); //true
System.out.println("p".matches("[a-d[m-p]]")); //true
System.out.println("e".matches("[a-d[m-p]]")); //false
System.out.println("0".matches("[a-d[m-p]]")); //false
// [a-z&&[def]] a-z和def的交集。为:d,e,f
System.out.println("----------5------------");
System.out.println("a".matches("[a-z&[def]]")); //false
System.out.println("d".matches("[a-z&&[def]]")); //true
System.out.println("0".matches("[a-z&&[def]]")); //false
// [a-z&&[^bc]] a-z和非bc的交集。(等同于[ad-z])
System.out.println("-----------6------------_");
System.out.println("a".matches("[a-z&&[^bc]]"));//true
System.out.println("b".matches("[a-z&&[^bc]]")); //false
System.out.println("0".matches("[a-z&&[^bc]]")); //false
// [a-z&&[^m-p]] a到z和除了m到p的交集。(等同于[a-1q-z])
System.out.println("-----------7-------------");
System.out.println("a".matches("[a-z&&[^m-p]]")); //true
System.out.println("m".matches("[a-z&&[^m-p]]")); //false
System.out.println("0".matches("[a-z&&[^m-p]]")); //false
2. 正则表达式---预字符类(也是匹配一个字符):
System.out.println(""")// 报错
System.out.println("\"");// "
System.out.println("\\");// \
回过 头来:
- 代码理解:
//.表示任意一个字符
System.out.println("你".matches("..")); //false
System.out.println("你".matches(".")); //true
System.out.println("你a".matches(".."));//true
// \\d 表示任意的一个数字(只能是一位)
// 简单来记:两个\表示一个\
System.out.println("a".matches("\\d")); // false
System.out.println("3".matches("\\d")); // true
System.out.println("333".matches("\\d")); // false
//\\w只能是一位单词字符[a-zA-Z_0-9]
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("A9".matches("\\w\\w"));//true
// 非单词字符
System.out.println("你".matches("\\W")); // true
System.out.println("---------------------------------------------");
注意以上的正则匹配只能校验单个字符
3.正则表达式---数量词 (可以匹配多个字符):
注意要和前面的 X 配套使用
代码理解:
// 必须是数字 字母 下划线 至少 6位
System.out.println("4Aa0_A".matches("\\w{6,}"));//true
System.out.println("4Aa".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
正则表达式的练习 1:
需求 请编写正则表达式验证用户输入的手机号码是否满足要求。请编写正则表达式验证用户输入的座机号码是否满足要求。请编写正则表达式验证用户输入的邮箱号是否满足要求。
技巧:先拿着正确数据,将数据分成不同部分,
从左到右依次写规则
习题一:
示例:
String regex="[1][3-9]\\w{9}";
System.out.println("15327998608".matches(regex));//true
System.out.println("12179986090".matches(regex));//false
System.out.println("11179986090".matches(regex));//false
System.out.println("151799860909".matches(regex));//false
System.out.println("05179986090".matches(regex));//false
习题二:
String regex2="0\\d{2,3}-?[1-9]\\d{4-9}"
....
习题三:
String regex3="\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2-3}){1,2}"
....
注意这里的 ”点“的表示是
\\.
正则表达式的练习 2:
习题一:
用户名要求:
- 大小写字母、数字、下划线一共 4-16 位
String regex="\\w{4,16}";
...
习题二:
身份证 简单要求:
- 18 位
- 不以 0 开头
- 前 17 任意数
- 最后一位可以是数字、X、x
String regex1="[1-9]\\d{16}(\\d|X|x)";//1
String regex2="[1-9]\\d{16}[\\dx]";//2
...
String regex1="(?i)abc"//忽略abc大小写
System.out.println("ABc".matches(regex1));//true
String regex2="a(?i)bc"//忽略bc大小写
System.out.println("ABc".matches(regex1));//false
这时我们就可以改写上面的身份证检验练习:
String regex1="[1-9]\\d{16}(\\d|(?i)x)";
身份证的严格校验:
- 前六位:省份,市区,派出所等信息
- 第一位不能是 0
- 后五位任意
[1-9]\\d{5}
- 年份的前半段:18、19、20
- 年的后半段:任意数字出现两次
(18|19|20)\\d{2}
- 月份:01-09、或 10、11、12
(0[1-9] | 1[0-2])
- 日:01-09、10-19、21-29 、30、31
( 0[1-9] | [12] \\d | 3[01] )
- 后四位:前三位任意、最后一位可以是数字、X、x
\\d{3} [\\dXx]
最后拼接起来即可
正则表达式 小结:
正则表达式重要的是看得懂
本章介绍了正则表达式,以及规则、校验字符串是否满足规则的重要作用。
下篇介绍第二个重要作用:在一段文本中查找满足要求的内容