{"record":{"id":"c1970de947a57fc7","repo":"jhy/jsoup","slug":"pattern-syntax-error-c1970d","errorCode":null,"errorMessage":"Pattern syntax error: ","messagePattern":"Pattern syntax error: ","errorType":"validation","errorClass":"ValidationException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/jsoup/helper/Regex.java","lineNumber":43,"sourceCode":"        this.jdkPattern = jdkPattern;\n    }\n\n    /**\n     Compile a regex, using re2j if enabled and available; otherwise JDK regex.\n\n     @param regex the regex to compile\n     @return the compiled regex\n     @throws ValidationException if the regex is invalid\n     */\n    public static Regex compile(String regex) {\n        if (usingRe2j()) {\n            return Re2jRegex.compile(regex);\n        }\n\n        try {\n            return new Regex(Pattern.compile(regex));\n        } catch (PatternSyntaxException e) {\n            throw new ValidationException(\"Pattern syntax error: \" + e.getMessage());\n        }\n    }\n\n    /** Wraps an existing JDK Pattern (for API compat); doesn't switch */\n    public static Regex fromPattern(Pattern pattern) {\n        return new Regex(pattern);\n    }\n\n    /**\n     Checks if re2j is available (on classpath) and enabled (via system property).\n     @return true if re2j is available and enabled\n     */\n    public static boolean usingRe2j() {\n        return hasRe2j && wantsRe2j();\n    }\n\n    static boolean wantsRe2j() {\n        return Boolean.parseBoolean(System.getProperty(SharedConstants.UseRe2j, \"true\"));","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/jhy/jsoup/blob/9851ac5d9c576c6888910b5a51a2362bbc978959/src/main/java/org/jsoup/helper/Regex.java#L25-L61","documentation":"Regex.compile() first tries java.util.regex.Pattern.compile(regex). If the pattern text is not syntactically valid for the JDK regex engine, it catches PatternSyntaxException and rethrows it as a ValidationException whose message includes the engine's syntax diagnostic.","triggerScenarios":"Any call to Regex.compile(regex) (with the default engine, not the RE2J switch) where the string is malformed: unbalanced parentheses like \"(a|b\", dangling escapes like \"\\\\\", invalid quantifiers like \"*abc\", bad character classes like \"[z-a]\", or an illegal group reference.","commonSituations":"Regexes built by string concatenation with unescaped user input; patterns copied from PCRE/other flavors with unsupported syntax; accidental double-escaping when embedding a regex in Java source or JSON.","solutions":["Fix the pattern syntax using the message from the exception, which pinpoints the failing index","Escape metacharacters properly with Pattern.quote() for literal text","Test the pattern in isolation before wiring it in","If porting from RE2J/PCRE, remove constructs the JDK engine rejects"],"exampleFix":"// before\nRegex r = Regex.compile(\"(a|b\"); // unbalanced group\n// after\nRegex r = Regex.compile(\"(a|b)\");","handlingStrategy":"validation","validationCode":"try {\n    java.util.regex.Pattern.compile(regex);\n} catch (java.util.regex.PatternSyntaxException e) {\n    throw new IllegalArgumentException(\"Invalid regex: \" + e.getMessage());\n}","typeGuard":null,"tryCatchPattern":"try {\n    Regex r = Regex.compile(userRegex);\n} catch (ValidationException e) {\n    reportSyntaxErrorToUser(e.getMessage()); // includes engine diagnostic + index\n}","preventionTips":["Pre-validate user regexes with Pattern.compile before storing them","Use Pattern.quote() for literal user text embedded in patterns","Beware double-escaping when regexes pass through JSON/config","Test patterns from external sources in isolation first"],"tags":["regex","syntax","validation"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"9851ac5d9c576c6888910b5a51a2362bbc978959","analyzedAt":"2026-09-08T15:22:04.931Z","contentChangedAt":"2026-09-08T15:22:04.931Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}