Created
March 8, 2016 08:32
-
-
Save sanandrea/686a5a3d762e177d74fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Console; | |
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
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 = null; | |
try{ | |
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"); | |
} | |
}catch (Exception ex){ | |
if(pattern == null){ | |
console.format("Null" + pattern.toString()); | |
}else{ | |
console.format(pattern.toString()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment