forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartEndStringAnchorsLesson.java
More file actions
48 lines (41 loc) · 1.47 KB
/
Copy pathStartEndStringAnchorsLesson.java
File metadata and controls
48 lines (41 loc) · 1.47 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
package regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StartEndStringAnchorsLesson {
public static void main(String[] args) {
Pattern p = Pattern.compile("^a");
// Pattern p = Pattern.compile("^b");
// Pattern p = Pattern.compile("c$");
// Pattern p = Pattern.compile("b$");
Matcher m = p.matcher("abc");
while(m.find()) {
System.out.print(m.start() + " " + m.group() + " ");
}
System.out.println("");
Pattern p2 = Pattern.compile("\\d+");
//Pattern p2 = Pattern.compile("^\\d+$");
Matcher m2 = p2.matcher("ab4c");
if(m2.find()) {
System.out.println("it's a number");;
}
System.out.println("");
//^\s+
//\s+$
Pattern p3 = Pattern.compile("ne$", Pattern.MULTILINE);
// Pattern p3 = Pattern.compile("\\Ane\\Z", Pattern.MULTILINE);
//Pattern p3 = Pattern.compile("ne\\z", Pattern.MULTILINE);
Matcher m3 = p3.matcher("first line\nsecond line");
while(m3.find()) {
System.out.print(m3.start() + " " + m3.group() + " ");
}
System.out.println("");
"749\\n486\\n4".matches("^4$");
Pattern p4 = Pattern.compile("^\\d*$");
Matcher m4 = p4.matcher("");
if(m4.find()) {
System.out.println(m4.start());
System.out.println("".charAt(m4.start()));
}
System.out.println("");
}
}