forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRegexAnchor.java
More file actions
31 lines (22 loc) · 788 Bytes
/
JavaRegexAnchor.java
File metadata and controls
31 lines (22 loc) · 788 Bytes
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
package com.zetcode;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Anchors match positions of characters inside a given text.
public class JavaRegexAnchor {
public static void main(String[] args) {
List<String> sentences = Arrays.asList("I am looking for Jane.",
"Jane was walking along the rive.",
"Kate and Jane are close friends.");
Pattern p = Pattern.compile("^Jane");
for (String word : sentences) {
Matcher m = p.matcher(word);
if (m.find()) {
System.out.printf("%s matches%n", word);
} else {
System.out.printf("%s does not match%n", word);
}
}
}
}