Skip to content

Commit c98b181

Browse files
committed
Play with regular expressions.
1 parent 15bf1bf commit c98b181

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

Chapter13/RegexBoundaryMatch.png

22.9 KB
Loading

Chapter13/RegexDemo.png

32.8 KB
Loading

Chapter13/RegexTestHarness.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.io.Console;
2+
import java.util.regex.Pattern;
3+
import java.util.regex.Matcher;
4+
5+
// This demo comes from <a href="https://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html">Java Tutorial</a>
6+
public class RegexTestHarness{
7+
public static void main(String[] args){
8+
Console console = System.console();
9+
if(console==null){
10+
System.err.println("No console.");
11+
System.exit(1);
12+
}
13+
while(true){
14+
Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
15+
Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));
16+
17+
boolean found = false;
18+
while(matcher.find()){
19+
console.format("I found the text \"%s\" starting at index %d and ending at index %d.%n",
20+
matcher.group(), matcher.start(), matcher.end());
21+
found = true;
22+
}
23+
if(!found){
24+
console.format("No match found.%n");
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)