-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartEnd.java
More file actions
48 lines (45 loc) · 1.56 KB
/
Copy pathStartEnd.java
File metadata and controls
48 lines (45 loc) · 1.56 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
import java.util.regex.*;
public class StartEnd{
public static String input =
"As long as there is injustice, whenever a\n" +
"Targathian baby cries out, wherever a distress\n" +
"signal sounds among the stars ... We'll be there.\n" +
"This fine ship, and this fine crew ...\n" +
"Never give up! Never surrender!";
private static class Display{
private boolean regexPrinted = false;
private String regex;
Display(String regex){
this.regex = regex;
}
void display(String message){
if(!regexPrinted){
System.out.println(regex);
regexPrinted = true;
}
System.out.println(message);
}
}
static void examine(String s, String regex){
Display d = new Display(regex);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while(m.find()){
d.display("find() '" + m.group() + "' start = " + m.start() + " end = " + m.end());
}
if(m.lookingAt()){
d.display("lookingAt() start = " + m.start() + " end = " + m.end());
}
if(m.matches()){
d.display("matches() start = " + m.start() + " end = " + m.end());
}
}
public static void main(String[] args){
for(String in : input.split("\n")){
System.out.println("input : " + in);
for(String regex : new String[]{"\\w*ere\\w*", "\\w*ever", "T\\w+", "Never.*?!"}){
examine(in, regex);
}
}
}
}