Java对于正则的支持:
- String类中,很多方法是专门用来支持正则:
- split()
- replaceAll()
- replaceFirst()
- matches()
java.util.regex包有两个类:Pattren类、Matcher类
代码示例:
@Test
void test01() {
String s = "this is a book,他今年12岁,今年2022年";
//replace方法不支持正则
String s1 = s.replace("\\d+", "22");
//使用replaceAll,和replace方法作用一模一样,就是多了支持正则的功能
String s1 = s.replaceAll("\\d+", "22");
System.out.println(s1);
//split方法
String[] arr = s.split("\\s+");
for (String ss: arr) {
System.out.println(ss);
}
//replaceFirst,只替换正则匹配中第一个符号
String s1 = s.replaceFirst("20", "20");
System.out.println(s1);
// matches方法,判断字符串是否符合某种规则
String s2 = "10086";
System.out.println(s2.matches("\\d+"));
}
元字符:. 匹配所有符号(除换行符\n) .转义
- \d 匹配所有的数字(0-9)
- \w 匹配所有的有效符号(数字、大小写字母、下划线、不认$和中文)
- \s 匹配空白位,包括空格 制表符\t
- [0123456789] 表示一位,列举中括号中的某一个符号
- [0-9] 表示0到9
- [A-Za-z0-9]
代码示例:
@Test
void test02() {
System.out.println("123".matches("..."));
System.out.println("a".matches("."));
System.out.println("A".matches("."));
System.out.println("$".matches("."));
System.out.println("+".matches("."));
System.out.println("-".matches("."));
// . 不能匹配\n
System.out.println("\n".matches("."));
}
@Test
void test03() {
System.out.println("123".matches("\\d\\d\\d"));
System.out.println("a12".matches("\\d\\d\\d"));
System.out.println("a12".matches("\\w\\w\\w"));
System.out.println("a1A".matches("\\w\\w\\w"));
System.out.println("$$A".matches("\\w\\w\\w"));
System.out.println("__2".matches("\\w\\w\\w"));
System.out.println("中国人".matches("\\w\\w\\w"));
}
@Test
void test04() {
System.out.println(" ".matches("\\s\\s"));
System.out.println("\t".matches("\\s"));
System.out.println("3".matches("[0123456789]"));
System.out.println("0".matches("[0123456789]"));
System.out.println("a".matches("[0123456789]"));
System.out.println("G".matches("[0123456789]"));
System.out.println("abc".matches("[abcdef]bc"));
System.out.println("zbc".matches("[abcdef]bc"));
System.out.println("kbc".matches("[ka]bc"));
}
^正则 //以什么时候开头,匹配字符串的开始
$正则 //以什么结尾匹配字符串的结尾
反义符:
\D 非数字
\W 特殊符号
\S 非空白位
^[abc] 以abc中的某一个字符开头
代码示例:
@Test
void test08() {
System.out.println("2asdfasdf".matches("^[123].*"));
System.out.println("123asdfasdf".matches("[^123].*"));
System.out.println("liujianhong123asdfasdf".matches("liujianhong[^123].*"));
System.out.println("liujianhong9123asdfasdf".matches("liujianhong[^123].*"));
}
位数问题:
.* //0~到多
? //0或者1 * f + //1~多 * {m} //固定的m个 * {m,} //至少m个 * {m,n} //在m~n区间内
转义符:在正则中,通过\将有特殊含义(\d)的符号转化为符号
分组:
"\\w{4,20}@openlab\.com"
( | )符号的使用:在匹配时,有多种可能的词组(如果单个字母,直接使用[]即可),我们可以使用|来表示选择其中某一个的。
|必须配合()使用
分组:分组是正则中的二次筛选
代码示例:
@Test
void test10() {
System.out.println("liujianhong@openlab.com".matches("\\w{4,20}@openlab\\.com"));
System.out.println("liujianhong@openlab.com".matches("\\w{4,20}@openlab\\.(com|org|gmail|cn|gov)"));
System.out.println("liujianhong@openlab.org".matches("\\w{4,20}@openlab\\.(com|org|gmail|cn|gov)"));
System.out.println("liujianhong@openlab.cn".matches("\\w{4,20}@openlab\\.(com|org|gmail|cn|gov)"));
System.out.println("liujianhong@openlab.qq.com".matches("\\w{4,20}@openlab\\.(com|org|gmail|cn|gov)"));
}
@Test
void test11() {
String msg = "<img src='https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newxueshuicon-a5314d5c83.png'/>";
System.out.println(msg.matches("<img.*src='(.*)'/>"));
}
@Test
void test12() {
String msg = "<img src='https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newxueshuicon-a5314d5c83.png'/>";
Pattern pattern = Pattern.compile("<img.*(src)='(.*)'/>");
Matcher matcher = pattern.matcher(msg);
while (matcher.find()) {
System.out.println(matcher.group(2));
}
}
贪婪模式和懒惰模式
贪婪模式:会尽可能多的匹配能够匹配的数据
懒惰模式:(非贪婪模式)会尽可能少的匹配能够匹配的数据
绝大多数编程默认采用贪婪模式,包括Java,python,等,将贪婪模式转化为懒惰模式