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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package _11zhengze;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

static boolean isValidMobileNumber(String s) {
return s.matches("\\d{11}");
}
public static void main(String[] args) {
//判断是否是电话号码
System.out.println(isValidMobileNumber("18640274769"));

//判断是否是20##年
String regex = "20\\d\\d";
System.out.println("2019".matches(regex)); // true
System.out.println("2100".matches(regex)); // false

//精确匹配 match
String re1 = "abc";
System.out.println("abc".matches(re1));
System.out.println("Abc".matches(re1));
System.out.println("abcd".matches(re1));

String re2 = "a\\&c"; // 对应的正则是a\&c
System.out.println("a&c".matches(re2));
System.out.println("a-c".matches(re2));
System.out.println("a&&c".matches(re2));

//匹配任意字符
String re3 = "a.c";
System.out.println("abc".matches(re3));
System.out.println("aBc".matches(re3));

//匹配数字
String re4 = "00\\d";
System.out.println("007".matches(re4));
System.out.println("00A".matches(re4));

//匹配常用字符 不能匹配#和空格
String re5 = "java\\w";
System.out.println("javac".matches(re5));
System.out.println("java9".matches(re5));
System.out.println("java_".matches(re5));
System.out.println("java#".matches(re5));
System.out.println("java ".matches(re5));

//匹配空格字符 包括空格和tab
String re6 = "a\\sc";
System.out.println("a c".matches(re6));
System.out.println("a\tc".matches(re6));

//匹配非数字 类似 \W \S
String re7 = "java\\d"; // 对应的正则是java\d
System.out.println("java9".matches(re7));
System.out.println("java10".matches(re7));
System.out.println("javac".matches(re7));

String re8 = "java\\D";
System.out.println("javax".matches(re8));
System.out.println("java#".matches(re8));
System.out.println("java5".matches(re8));

//重复匹配
String re9 = "A\\d*"; //匹配任意字符,包括0个
System.out.println("A".matches(re9));
System.out.println("A380".matches(re9));

String re10 = "A\\d+"; //匹配至少一个字符
System.out.println("A".matches(re10));
System.out.println("A380".matches(re10));

String re11 = "A\\d?"; //匹配0个或一个
System.out.println("A".matches(re11));
System.out.println("A3".matches(re11));
System.out.println("A380".matches(re11));

//匹配指定个数字符
String re12 = "A\\d{3}"; //3个
System.out.println("A380".matches(re12));
String re13 = "A\\d{3,5}"; //3到5个
System.out.println("A380".matches(re13));
System.out.println("A3801".matches(re13));
String re14 = "A\\d{3,}"; //至少三个
System.out.println("A38023421".matches(re14));

/**复杂匹配规则*/
//^匹配开头和$结尾
String re15 = "^A\\d{3}$"; //至少三个
System.out.println("A380".matches(re15));

//匹配指定范围
String re16 = "[123456789]\\d{6,7}";
String re17 = "[1...9]\\d{6,7}";
System.out.println("1864027".matches(re16));
System.out.println("1864027".matches(re17));
String re18 = "[0-9a-fA-F]{6}"; //匹配0-9,a-f,A-F
System.out.println("1A2b3c".matches(re18));
String re19 = "[^1-9]{3}"; //排除法,不包含1-9
System.out.println("ABC".matches(re19));
System.out.println("AB1".matches(re19));

//或规则匹配
String re20 = "java|php";
System.out.println("java".matches(re20));
System.out.println("php".matches(re20));
System.out.println("go".matches(re20));


//使用括号 重复部分
String re21 = "learn\\s(java|php|go)";
System.out.println("learn java".matches(re21));
System.out.println("learn Java".matches(re21));
System.out.println("learn php".matches(re21));
System.out.println("learn Go".matches(re21));

//分组匹配
Pattern pattern = Pattern.compile("(\\d{3,4})\\-(\\d{7,8})");
pattern.matcher("010-12345678").matches(); // true
pattern.matcher("021-123456").matches(); // true
pattern.matcher("022#1234567").matches(); // false
// 获得Matcher对象:
Matcher matcher = pattern.matcher("010-12345678");
if (matcher.matches()) {
String whole = matcher.group(0); // "010-12345678", 0表示匹配的整个字符串
String area = matcher.group(1); // "010", 1表示匹配的第1个子串
String tel = matcher.group(2); // "12345678", 2表示匹配的第2个子串
System.out.println(whole);
System.out.println(area);
System.out.println(tel);
}

//非贪婪匹配 尽可能少的匹配
Pattern pattern1 = Pattern.compile("(\\d+?)(0*)");
Matcher matcher1 = pattern1.matcher("1230000");
if (matcher1.matches()) {
System.out.println("group1=" + matcher1.group(1)); // "123"
System.out.println("group2=" + matcher1.group(2)); // "0000"
}

//分割字符串
String[] s = "a, b ;; c".split("[\\,\\;\\s]+");
System.out.println(s[0]+s[1]+s[2]);

//搜索字符串
String s1 = "the quick brown fox jumps over the lazy dog.";
Pattern p = Pattern.compile("\\wo\\w");
Matcher m = p.matcher(s1);
while (m.find()) {
String sub = s1.substring(m.start(), m.end());
System.out.println(sub);
}

//替换字符串
String s2 = "The quick\t\t brown fox jumps over the lazy dog.";
String r = s2.replaceAll("\\s+", " ");
System.out.println(r); // "The quick brown fox jumps over the lazy dog."

//反向引用
String s3 = "the quick brown fox jumps over the lazy dog.";
String r3 = s3.replaceAll("\\s([a-z]{4})\\s", " <b>$1</b> ");
System.out.println(r3);
}
}

单个字符的匹配规则如下:

正则表达式 规则 可以匹配
A 指定字符 A
\u548c 指定Unicode字符
. 任意字符 ab&0
\d 数字0~9 0~9
\w 大小写字母,数字和下划线 a~zA~Z0~9_
\s 空格、Tab键 空格,Tab
\D 非数字 aA&_,……
\W 非\w &@,……
\S 非\s aA&_,……

多个字符的匹配规则如下:

正则表达式 规则 可以匹配
A* 任意个数字符 空,AAAAAA,……
A+ 至少1个字符 AAAAAA,……
A? 0个或1个字符 空,A
A{3} 指定个数字符 AAA
A{2,3} 指定范围个数字符 AAAAA
A{2,} 至少n个字符 AAAAAAAAA,……
A{0,3} 最多n个字符 空,AAAAAA

复杂匹配规则主要有:

正则表达式 规则 可以匹配
^ 开头 字符串开头
$ 结尾 字符串结束
[ABC] […]内任意字符 A,B,C
[A-F0-9xy] 指定范围的字符 A,……,F0,……,9xy
[^A-F] 指定范围外的任意字符 A~F
AB|CD|EF AB或CD或EF ABCDEF