jhy/jsoup · error · ValidationException

Pattern syntax error:

Error message

Pattern syntax error: 

What it means

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.

Solutions

  1. Fix the pattern syntax using the message from the exception, which pinpoints the failing index
  2. Escape metacharacters properly with Pattern.quote() for literal text
  3. Test the pattern in isolation before wiring it in
  4. If porting from RE2J/PCRE, remove constructs the JDK engine rejects

Example fix

// before
Regex r = Regex.compile("(a|b"); // unbalanced group
// after
Regex r = Regex.compile("(a|b)");
Defensive patterns

Strategy: validation

Validate before calling

try {
    java.util.regex.Pattern.compile(regex);
} catch (java.util.regex.PatternSyntaxException e) {
    throw new IllegalArgumentException("Invalid regex: " + e.getMessage());
}

Try / catch

try {
    Regex r = Regex.compile(userRegex);
} catch (ValidationException e) {
    reportSyntaxErrorToUser(e.getMessage()); // includes engine diagnostic + index
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jhy/jsoup@9851ac5d9c (2026-09-08). Data as JSON: /api/errors/c1970de947a57fc7. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/org/jsoup/helper/Regex.java:43

        this.jdkPattern = jdkPattern;
    }

    /**
     Compile a regex, using re2j if enabled and available; otherwise JDK regex.

     @param regex the regex to compile
     @return the compiled regex
     @throws ValidationException if the regex is invalid
     */
    public static Regex compile(String regex) {
        if (usingRe2j()) {
            return Re2jRegex.compile(regex);
        }

        try {
            return new Regex(Pattern.compile(regex));
        } catch (PatternSyntaxException e) {
            throw new ValidationException("Pattern syntax error: " + e.getMessage());
        }
    }

    /** Wraps an existing JDK Pattern (for API compat); doesn't switch */
    public static Regex fromPattern(Pattern pattern) {
        return new Regex(pattern);
    }

    /**
     Checks if re2j is available (on classpath) and enabled (via system property).
     @return true if re2j is available and enabled
     */
    public static boolean usingRe2j() {
        return hasRe2j && wantsRe2j();
    }

    static boolean wantsRe2j() {
        return Boolean.parseBoolean(System.getProperty(SharedConstants.UseRe2j, "true"));

View on GitHub (pinned to 9851ac5d9c)