Skip to content

Commit d1a5183

Browse files
Java Examples
1 parent dc842fd commit d1a5183

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.howtodoinjava.regex;
2+
3+
import java.util.List;
4+
import java.util.regex.*;
5+
6+
public class StartsWithEndsWith {
7+
8+
public static void main(String[] args) {
9+
10+
List<String> lines = List.of(
11+
"The cat is cute",
12+
"The category is empty",
13+
"The noncategory is also empty");
14+
15+
Pattern pattern = Pattern.compile("\\bcat\\b", Pattern.CASE_INSENSITIVE);
16+
17+
for(String line: lines) {
18+
Matcher matcher = pattern.matcher(line);
19+
while (matcher.find()) {
20+
//System.out.println(STR."Match found: \{matcher.group()}");
21+
}
22+
}
23+
24+
pattern = Pattern.compile("\\b\\w*cat\\w*\\b");
25+
26+
for(String line: lines) {
27+
Matcher matcher = pattern.matcher(line);
28+
while (matcher.find()) {
29+
System.out.println(STR."Match found: \{matcher.group()}");
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)