try{ Pattern.compile(pattern); System.out.println("Valid"); } catch(PatternSyntaxException e) { System.out.println("Invalid"); }