/
Example15.java
56 lines (40 loc) · 1.69 KB
/
Example15.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
package reg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example15 {
public static void main(String args[]) {
final String case1 = "One ring to bring them all and in the darkness bind them";
final Pattern pattern1 = Pattern.compile(".{5}");
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()); //One r, ing t,o bri,ng th,em al,l and, in t,he da,rknes,s bin,d the
}
result1 = matcher1.find();
}
final String case2 = "One ring to bring them all and in the darkness bind them";
final Pattern pattern2 = Pattern.compile("[els]{1,3}");
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()); //e,e,ll,e,ess,e
}
result2 = matcher2.find();
}
final String case3 = "One ring to bring them all and in the darkness bind them";
final Pattern pattern3 = Pattern.compile("[a-z]{3,}");
final Matcher matcher3 = pattern3.matcher(case3);
boolean result3 = matcher3.find();
while (result3) {
if (result3) { // true
System.out.println("Cas2e 3");
System.out.println(matcher3.group()); //ring,bring,them,all,and,the,darkness,bind,them
}
result3 = matcher3.find();
}
}
}