/
Example14.java
58 lines (40 loc) · 1.58 KB
/
Example14.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
package reg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example14 {
public static void main(String args[]) {
final String case1 = "--XX-@-XX-@@-XX-@@@-XX-@@@@-XX-@@-@@-";
final Pattern pattern1 = Pattern.compile("-X?XX?X");
final Matcher matcher1 = pattern1.matcher(case1);
boolean result1 = matcher1.find();
while (result1) {
if (result1) { // true
System.out.println("Case 1");
System.out.println(matcher1.group()); //-XX,-XX,-XX,-XX,-XX
}
result1 = matcher1.find();
}
final String case2 = "--XX-@-XX-@@-XX-@@@-XX-@@@@-XX-@@-@@-";
final Pattern pattern2 = Pattern.compile("-@?@?@?-");
final Matcher matcher2 = pattern2.matcher(case2);
boolean result2 = matcher2.find();
while (result2) {
if (result2) { // true
System.out.println("Case 2");
System.out.println(matcher2.group()); //--,-@-,-@@-,-@@@-,-@@-
}
result2 = matcher2.find();
}
final String case3 = "--XX-@-XX-@@-XX-@@@-XX-@@@@-XX-@@-@@-";
final Pattern pattern3 = Pattern.compile("[^@]@?@");
final Matcher matcher3 = pattern3.matcher(case3);
boolean result3 = matcher3.find();
while (result3) {
if (result3) { // true
System.out.println("Case 3");
System.out.println(matcher3.group()); //-@,-@@,-@@,-@@,-@@,-@@
}
result3 = matcher3.find();
}
}
}