Java中正则表达式运用实例(参看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
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
|
package test; /** * 在String的matches()方法,split()方法中使用正则表达式. * @author fhd001 */ public class RegexTest { public static void main(String[] args) { /* * 普通字符 */ String str1 = "abc45abc345"; String[]arr1 = str1.split("abc"); for (String string : arr1) { System.out.print(string+"-->"); } System.out.println(); /* * 简单的转义字符 * 在java中转义字符时一定要用双反斜线,两个斜线转义成一个斜线, * 在用这个斜线字符转义那些特殊字符. */ String str2 = "^$()[]{}.?+*|"; boolean flag = str2.matches("//^//$//(//)//[//]//{//}//.//?//+//*//|"); System.out.println(flag); /* * 转义字符 /Q.../E. */ String str3 = "^$()[]{}.?+*|99999"; boolean flag2 = str3.matches("//Q^$()[]{}.?+*|//E//d{5}"); System.out.println(flag2); /* * 字符集合 */ String str4 = "dfddri334"; boolean flag4 = str4.matches(".+"); System.out.println(flag4); String str5 = "#$%^*())%"; boolean flag5 = str5.matches("//W{9}"); System.out.println(flag5); String str6 = "4gffmdkekrhhr"; boolean flag6 = str6.matches("//w+"); System.out.println(flag6); String str7 = "fjfdke eett "; boolean flag7 = str7.matches("//w+//s+//w{4}//s?"); System.out.println(flag7); String str8 = " erefff "; boolean flag8 = str8.matches("//s+//S+//s+"); System.out.println(flag8); String str9 = "456776888"; boolean flag9 = str9.matches("//d+"); System.out.println(flag9); String str10 = "rtydfgrgwvr "; boolean flag10 = str10.matches("//D+"); System.out.println(flag10); /* * 自定义字符集合 [ ] */ String str11 = "fdfeetg 34566"; boolean flag11 = str11.matches("[fdetg]+//s+[3-6]+"); System.out.println(flag11); String str12 = "rtyuie 5768"; boolean flag12 = str12.matches("[^abcdf]+//s+[^1234]+"); System.out.println(flag12); /* * 匹配次数限定符 */ //贪婪模式 String str13 = "ytreggcv454444444333"; boolean flag13 = str13.matches("//w{20}"); System.out.println(flag13); boolean flag14 = str13.matches("//w{10,21}"); System.out.println(flag14); boolean flag15 = str13.matches("//w{18,}"); System.out.println(flag15); String str14 = "4"; boolean flag16 = str14.matches("//d?"); System.out.println(flag16); String str15 = "ddcvgt"; boolean flag17 = str15.matches("//D+//d?"); System.out.println(flag17); String str16 = "e33tf44t44t"; boolean flag18 = str16.matches("//w+//W*"); System.out.println(flag18); //勉强模式(只举一例) String str17 = "34567ghjkkld"; boolean flag19 = str17.matches("//d{2,7}?//w{8,11}"); System.out.println(flag19); //占有模式(只举一例) String str18 = "22222ddddd"; boolean flag20 = str18.matches("//d{2,5}+//w{6}"); System.out.println(flag20); /* * 字符边界 */ String str19 = "a444545rot44tm"; boolean flag21 = str19.matches("^a//w+m$"); System.out.println(flag21); /* * 选择表达式 xxx | xxx */ String str20 = "abc123abc"; boolean flag22 = str20.matches("(abc|123){3}"); System.out.println(flag22); /* * 分组 ( ). * 以上修饰符都是针对一个字符进行修饰.如果要对一组字符进行 * 修饰就要用到() */ String str21 = "123qwe123qwe"; boolean flag23 = str21.matches("(123qwe){2}"); System.out.println(flag23); /* * []中的交集与并集 */ String str22 = "abcdefgh1234567" ; boolean flag24 = str22.matches( "[a-z1-9]+" ); //并集 System.out.println(flag24); boolean flag25 = str22.matches( "[a-z1-9&&[a-h1-7]]+" ); //交集 System.out.println(flag25); } } |
结果代码
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
|
--> 45 --> 345 --> true true true true true true true true true true true true true true true true true true false true true true true true |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/vrix/article/details/5713420