forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegExpLesson.java
More file actions
91 lines (79 loc) · 2.79 KB
/
Copy pathRegExpLesson.java
File metadata and controls
91 lines (79 loc) · 2.79 KB
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
package regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by max on 3/4/17.
*/
public class RegExpLesson {
public static void main(String[] args) {
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher("abaaaba");
while(m.find()) {
System.out.print(m.start() + " " + m.group() + " ");
}
System.out.println("");
Pattern p2 = Pattern.compile("aba");
Matcher m2 = p2.matcher("abababa");
while(m2.find()) {
System.out.print(m2.start() + " " + m2.group() + " ");
}
System.out.println("");
//Pattern p3 = Pattern.compile("\\d");
Pattern p3 = Pattern.compile("\\d+");
Matcher m3 = p3.matcher("a12c3e456f");
while(m3.find()) {
System.out.print(m3.start() + " " + m3.group() + " ");
}
System.out.println("");
Pattern p4 = Pattern.compile("\\w");
Matcher m4 = p4.matcher("a 1 56 _Z");
while(m4.find()) {
System.out.print(m4.start() + " " + m4.group() + " ");
}
System.out.println("");
Pattern p5 = Pattern.compile("[a-c]");
Matcher m5 = p5.matcher("abc");
while(m5.find()) {
System.out.print(m5.start() + " " + m5.group() + " ");
}
System.out.println("");
Pattern p6 = Pattern.compile("proj1([^,])*");
Matcher m6 = p6.matcher("proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java");
while(m6.find()) {
System.out.print(m6.start() + " " + m6.group() + " ");
}
System.out.println("");
Pattern p7 = Pattern.compile("\\d\\d\\d([-\\s])?\\d\\d\\d\\d");
Matcher m7 = p7.matcher("123 4567");
while(m7.find()) {
System.out.print(m7.start() + " " + m7.group() + " ");
}
System.out.println("");
Pattern p8 = Pattern.compile("a.c");
Matcher m8 = p8.matcher("ac abc a c");
while(m8.find()) {
System.out.print(m8.start() + " " + m8.group() + " ");
}
System.out.println("");
//greedy
Pattern p9 = Pattern.compile(".*xx");
Matcher m9 = p9.matcher("yyxxxyxx");
while(m9.find()) {
System.out.print(m9.start() + " " + m9.group() + " ");
}
System.out.println("");
//reluctant
Pattern p10 = Pattern.compile(".*?xx");
Matcher m10 = p10.matcher("yyxxxyxx");
while(m10.find()) {
System.out.print(m10.start() + " " + m10.group() + " ");
}
System.out.println("");
//possessive
Pattern p11 = Pattern.compile(".*+xx");
Matcher m11 = p11.matcher("yyxxxyxx");
while(m11.find()) {
System.out.print(m11.start() + " " + m11.group() + " ");
}
}
}