正则表达式
字符类
- [abc] 代表a或者b或者c字符中的一个
- [^abc] 代表除a,b,c以外的任何字符
- [a-z] 代表a-z的所有小写字符中的一个
- [A-Z] 代表A-Z的所有大写字符中的一个
- [0-9] 代表0-9之间的某一个数字字符
- [a-zA-Z0-9] 代表a-z或者A-Z或者0-9之间的任意一个字符
- [a-dm-p] a到d或m到p之间的任意一个字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class RegexDemo2 { public static void main(String[] args) { System.out.println("-----------1-------------"); System.out.println("a".matches("[abc]")); System.out.println("z".matches("[abc]"));
System.out.println("-----------2-------------"); System.out.println("a".matches("[^abc]")); System.out.println("z".matches("[^abc]")); System.out.println("zz".matches("[^abc]")); System.out.println("zz".matches("[^abc][^abc]"));
System.out.println("-----------3-------------"); System.out.println("a".matches("[a-zA-z]")); System.out.println("z".matches("[a-zA-z]")); System.out.println("aa".matches("[a-zA-z]")); System.out.println("zz".matches("[a-zA-Z]")); System.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); System.out.println("0".matches("[a-zA-Z]")); System.out.println("0".matches("[a-zA-Z0-9]"));
System.out.println("-----------4-------------"); System.out.println("a".matches("[a-d[m-p]]")); System.out.println("d".matches("[a-d[m-p]]")); System.out.println("m".matches("[a-d[m-p]]")); System.out.println("p".matches("[a-d[m-p]]")); System.out.println("e".matches("[a-d[m-p]]")); System.out.println("0".matches("[a-d[m-p]]"));
System.out.println("----------5------------"); System.out.println("a".matches("[a-z&[def]]")); System.out.println("d".matches("[a-z&&[def]]")); System.out.println("0".matches("[a-z&&[def]]"));
System.out.println("-----------6------------_"); System.out.println("a".matches("[a-z&&[^bc]]")); System.out.println("b".matches("[a-z&&[^bc]]")); System.out.println("0".matches("[a-z&&[^bc]]"));
System.out.println("-----------7-------------"); System.out.println("a".matches("[a-z&&[^m-p]]")); System.out.println("m".matches("[a-z&&[^m-p]]")); System.out.println("0".matches("[a-z&&[^m-p]]"));
} }
|
逻辑运算符
- && :并且
- | : 或者
- \ : 转义字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class Demo { public static void main(String[] args) { String str = "had"; String regex = "[a-z&&[^aeiou]]ad"; System.out.println("1." + str.matches(regex)); regex = "[a|e|i|o|u]ad"; System.out.println("2." + str.matches(regex)); }
System.out.println("\"");
System.out.println("c:Users\\moon\\IdeaProjects\\basic-code\\myapi\\src\\com\\itheima\\a08regexdemo\\RegexDemo1.java"); }
|
预定义字符
- “.” :匹配任何字符
- “\d” :任何数字[0-9]的简写
- “\D” : 任何非数字[^0-9]的简写
- “\s” : 空白字符 [\t\n\x0B\f\r]的简写
- “\S” : 非空白字符 :[^\s]的简写
- “\w” :单词字符:[a-zA-Z_0-9]的简写
- “\W” : 非单词字符:[^\w]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class Demo { public static void main(String[] args) { System.out.println("你".matches("..")); System.out.println("你".matches(".")); System.out.println("你a".matches(".."));
System.out.println("a".matches("\\d")); System.out.println("3".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("你".matches("\\W")); System.out.println("---------------------------------------------");
System.out.println("2442fsfsf".matches("\\w{6,}")); System.out.println("244f".matches("\\w{6,}"));
System.out.println("23dF".matches("[a-zA-Z0-9]{4}")); System.out.println("23 F".matches("[a-zA-Z0-9]{4}")); System.out.println("23dF".matches("[\\w&&[^_]]{4}")); System.out.println("23_F".matches("[\\w&&[^_]]{4}")); } }
|
数量词
- X? :0次或1次
- X*:0次到多次
- X+:1次或多次
- X{n} :恰好n次
- X{n,} : 至少n次
- X{n,m}:n到m次(n和m都是包含的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Demo { public static void main(String[] args) { System.out.println("2442fsfsf".matches("\\w{6,}")); System.out.println("244f".matches("\\w{6,}"));
System.out.println("23dF".matches("[a-zA-Z0-9]{4}")); System.out.println("23 F".matches("[a-zA-Z0-9]{4}")); System.out.println("23dF".matches("[\\w&&[^_]]{4}")); System.out.println("23_F".matches("[\\w&&[^_]]{4}")); } }
|
练习
请编写正则表达式验证用户输入的手机号码是否满足要求。
请编写正则表达式验证用户输入的邮箱号是否满足要求。
请编写正则表达式验证用户输入的电话号码是否满足要求。
验证手机号码 13112345678 13712345667 13945679027 139456790271
验证座机电话号码 020-2324242 02122442 027-42424 0712-3242434
验证邮箱号码 3232323@qq.com zhangsan@itcast.cnn dlei0009@163.com dlei0009@pci.com.cn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class RegexDemo4 { public static void main(String[] args) { String regex1 = "1[3-9]\\d{9}"; System.out.println("13112345678".matches(regex1)); System.out.println("13712345667".matches(regex1)); System.out.println("13945679027".matches(regex1)); System.out.println("139456790271".matches(regex1)); System.out.println("-----------------------------------");
String regex2 = "0\\d{2,3}-?[1-9]\\d{4,9}"; System.out.println("020-2324242".matches(regex2)); System.out.println("02122442".matches(regex2)); System.out.println("027-42424".matches(regex2)); System.out.println("0712-3242434".matches(regex2));
String regex3 = "\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2}"; System.out.println("3232323@qq.com".matches(regex3)); System.out.println("zhangsan@itcast.cnn".matches(regex3)); System.out.println("dlei0009@163.com".matches(regex3)); System.out.println("dlei0009@pci.com.cn".matches(regex3));
String regex4 = "([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d"; System.out.println("23:11:11".matches(regex4));
String regex5 = "([01]\\d 2[0-3])(:[0-5]\\d){2}"; System.out.println("23:11:11".matches(regex5)); } }
|
本地数据爬取
Pattern:表示正则表达式
Matcher:文本匹配器,作用按照正则表达式的规则去读取字符串,从头开始读取。
在大串中去找符合匹配规则的子串。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.itheima.a08regexdemo;
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class RegexDemo6 { public static void main(String[] args) {
String str = "Java自从95年问世以来,经历了很多版本,目前企业中用的最多的是Java8和Java11," + "因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台";
Pattern p = Pattern.compile("Java\\d{0,2}"); Matcher m = p.matcher(str);
while (m.find()) { String s = m.group(); System.out.println(s); }
}
private static void method1(String str) {
Pattern p = Pattern.compile("Java\\d{0,2}"); Matcher m = p.matcher(str);
boolean b = m.find();
String s1 = m.group(); System.out.println(s1);
b = m.find();
String s2 = m.group(); System.out.println(s2); } }
|
网络数据爬取
需求:
把连接:https://m.sengzan.com/jiaoyu/29104.html?ivk sa=1025883i中所有的身份证号码都爬取出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class RegexDemo7 { public static void main(String[] args) throws IOException {
URL url = new URL("https://m.sengzan.com/jiaoyu/29104.html?ivk sa=1025883i"); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; String regex = "[1-9]\\d{17}"; Pattern pattern = Pattern.compile(regex); while ((line = br.readLine()) != null) { Matcher matcher = pattern.matcher(line); while (matcher.find()) { System.out.println(matcher.group()); } } br.close(); } }
|
按要求爬取
需求:
有如下文本,按要求爬取数据。
Java自从95年问世以来,经历了很多版本,目前企业中用的最多的是Java8和Java11,因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台。
需求1:
爬取版本号为8,11.17的Java文本,但是只要Java,不显示版本号。
需求2:
爬取版本号为8,11,17的Java文本。正确爬取结果为:Java8 Java11 Java17 Java17
需求3:
爬取除了版本号为8,11,17的Java文本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class RegexDemo9 { public static void main(String[] args) {
String s = "Java自从95年问世以来,经历了很多版本,目前企业中用的最多的是Java8和Java11," + "因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台";
String regex1 = "((?i)Java)(?=8|11|17)"; String regex2 = "((?i)Java)(8|11|17)"; String regex3 = "((?i)Java)(?:8|11|17)"; String regex4 = "((?i)Java)(?!8|11|17)";
Pattern p = Pattern.compile(regex4); Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group()); } } }
|
贪婪爬取和非贪婪爬取
1 2 3 4 5 6 7 8 9 10
| 只写+表示贪婪匹配,如果在+和后面加问号表示非贪婪爬取 +? 非贪婪匹配 *? 非贪婪匹配 贪婪爬取:在爬取数据的时候尽可能的多获取数据 非贪婪爬取:在爬取数据的时候尽可能的少获取数据
举例: 如果获取数据:ab+ 贪婪爬取获取结果:abbbbbbbbbbbb 非贪婪爬取获取结果:ab
|
String的split方法中使用正则表达式
String类的split()方法原型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public String[] split(String regex)
String s = "小诗诗dqwefqwfqwfwq12312小丹丹dqwefqwfqwfwq12312小惠惠";
String[] arr = s.split("[\\w&&[^_]]+"); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
|
String类的replaceAll方法使用正则表达式
String类的replaceAll方法原型
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public String replaceAll(String regex,String newStr)
String s = "小诗诗dqwefqwfqwfwq12312小丹丹dqwefqwfqwfwq12312小惠惠";
String result1 = s.replaceAll("[\\w&&[^_]]+", "vs"); System.out.println(result1);
|
分组括号
细节:如何识别组号?
只看左括号,不看右括号,按照左括号的顺序,从左往右,依次为第一组,第二组,第三组等等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
String regex1 = "(.).+\\1"; System.out.println("a123a".matches(regex1)); System.out.println("b456b".matches(regex1)); System.out.println("17891".matches(regex1)); System.out.println("&abc&".matches(regex1)); System.out.println("a123b".matches(regex1)); System.out.println("--------------------------");
String regex2 = "(.+).+\\1"; System.out.println("abc123abc".matches(regex2)); System.out.println("b456b".matches(regex2)); System.out.println("123789123".matches(regex2)); System.out.println("&!@abc&!@".matches(regex2)); System.out.println("abc123abd".matches(regex2)); System.out.println("---------------------");
String regex3 = "((.)\\2*).+\\1"; System.out.println("aaa123aaa".matches(regex3)); System.out.println("bbb456bbb".matches(regex3)); System.out.println("111789111".matches(regex3)); System.out.println("&&abc&&".matches(regex3)); System.out.println("aaa123aab".matches(regex3));
|
分组练习
需求:
将字符串:我要学学编编编编程程程程程程。
替换为:我要学编程
1 2 3 4 5 6 7 8 9 10 11 12
| String str = "我要学学编编编编程程程程程程";
String result = str.replaceAll("(.)\\1+", "$1"); System.out.println(result);
|
忽略大小写的写法
1 2 3 4 5 6 7
|
String regex = "(?i)abc";
String regex = "a(?i)bc";
String regex = "a((?i)b)c";
|
非捕获分组
非捕获分组:分组之后不需要再用本组数据,仅仅是把数据括起来
1 2 3 4 5 6 7 8 9 10 11 12
|
String regex2 ="[1-9]\\d{16}(\\d Xx)\\1";
System.out.println("41080119930228457x".matches(regex2));
|