I have a little program allowing users to type-in some regular expressions. afterwards I like to check if this input is a valid regex or not.
I\'m w
Here is an example.
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class RegexTester {
public static void main(String[] arguments) {
String userInputPattern = arguments[0];
try {
Pattern.compile(userInputPattern);
} catch (PatternSyntaxException exception) {
System.err.println(exception.getDescription());
System.exit(1);
}
System.out.println("Syntax is ok.");
}
}
java RegexTester "(capture" then outputs "Unclosed group", for example.