-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegexTestHarness.java
More file actions
28 lines (26 loc) · 892 Bytes
/
Copy pathRegexTestHarness.java
File metadata and controls
28 lines (26 loc) · 892 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
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
// This demo comes from <a href="https://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html">Java Tutorial</a>
public class RegexTestHarness{
public static void main(String[] args){
Console console = System.console();
if(console==null){
System.err.println("No console.");
System.exit(1);
}
while(true){
Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));
boolean found = false;
while(matcher.find()){
console.format("I found the text \"%s\" starting at index %d and ending at index %d.%n",
matcher.group(), matcher.start(), matcher.end());
found = true;
}
if(!found){
console.format("No match found.%n");
}
}
}
}