Skip to content

Instantly share code, notes, and snippets.

@melkorm
Created August 28, 2014 12:11
Show Gist options
  • Save melkorm/0f4f7d0514c851897ae2 to your computer and use it in GitHub Desktop.
Save melkorm/0f4f7d0514c851897ae2 to your computer and use it in GitHub Desktop.
Java RegEx testing class
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class RegexTester {
public static void main(String[] arguments) {
String userInputPattern = arguments[0];
System.err.println(userInputPattern);
String userInputText = arguments[1];
System.err.println(userInputText);
try {
Pattern p = Pattern.compile(userInputPattern);
boolean b = Pattern.matches(userInputPattern, userInputText);
System.err.println(b ? "Matches" : "Nope :(");
} catch (PatternSyntaxException exception) {
System.err.println(exception.getDescription());
System.exit(1);
}
System.out.println("Syntax is ok.");
}
}
How to run (OSx):
javac RegexTester.java
java RegexTester "regex" "string to match"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment