forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRegexSubpatterns.java
More file actions
30 lines (21 loc) · 820 Bytes
/
JavaRegexSubpatterns.java
File metadata and controls
30 lines (21 loc) · 820 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
package com.zetcode;
// Subpatterns are patterns within patterns. They are created with () characters.
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexSubpatterns {
public static void main(String[] args) {
List<String> words = Arrays.asList("book", "bookshelf", "bookworm",
"bookcase", "bookish", "bookkeeper", "booklet", "bookmark");
Pattern p = Pattern.compile("book(worm|mark|keeper)?");
for (String word : words) {
Matcher m = p.matcher(word);
if (m.matches()) {
System.out.printf("%s matches%n", word);
} else {
System.out.printf("%s does not match%n", word);
}
}
}
}