forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharackterClassesLesson.java
More file actions
64 lines (54 loc) · 1.82 KB
/
Copy pathCharackterClassesLesson.java
File metadata and controls
64 lines (54 loc) · 1.82 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
package regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharackterClassesLesson {
public static void main(String[] args) {
Pattern p = Pattern.compile("gr[ae]y");
Matcher m = p.matcher("gray");
//Matcher m = p.matcher("grey");
while(m.find()) {
System.out.print(m.start() + " " + m.group() + " ");
}
System.out.println("");
//examples
//[0-9][a-z][a-zA-Z][0-9a-fA-F]
Pattern p2 = Pattern.compile("q[^u]");
Matcher m2 = p2.matcher("Iraq is a country");
//Matcher m2 = p2.matcher("Iraq");
while(m2.find()) {
System.out.print(m2.start() + " " + m2.group() + " ");
}
System.out.println("");
//special chars
//]\^-
//[\\x] [x^] [^]x] [-x] [x-]
//[\Q[-]\E]
Pattern p3 = Pattern.compile("[*+]");
Matcher m3 = p3.matcher("1+1=2");
while(m3.find()) {
System.out.print(m3.start() + " " + m3.group() + " ");
}
System.out.println("");
//\d - [0-9] \D-[^d\]
//\w[a-ZA-Z] \W-[^w\]
//\s[ \t] \S-[^s\]
//[\s\d] \s\d
//[\D\S]
//repeatCharacters
//?*+
Pattern p4 = Pattern.compile("[0-9]+");
Matcher m4 = p4.matcher("1 + 1 = 2");
while(m4.find()) {
System.out.print(m4.start() + " - " + m4.group() + ", ");
}
System.out.println("");
// Pattern p5 = Pattern.compile("([0-9])\\1+");
//// Matcher m5 = p5.matcher("222");
//// Matcher m5 = p5.matcher("876");
// Matcher m5 = p5.matcher("833337");
// while(m5.find()) {
// System.out.print(m5.start() + " - " + m5.group() + ", ");
// }
// System.out.println("");
}
}