jhy/jsoup · error · IllegalArgumentException

Pattern syntax error:

Error message

Pattern syntax error: 

What it means

getElementsByAttributeValueMatching(key, regex) compiles the supplied regex string into a Pattern. If the regex is syntactically invalid, PatternSyntaxException is wrapped and rethrown as IllegalArgumentException('Pattern syntax error: <regex>').

Solutions

  1. Fix the regex syntax; test it with Pattern.compile(regex) in isolation first
  2. Validate the pattern with Pattern.compile in a try-catch before passing it to jsoup
  3. If the source is user input, surface a validation error instead of executing the search
  4. Check escaping: a Java string literal needs double backslashes, e.g. "\\d+" for \d+

Example fix

// before
doc.getElementsByAttributeValueMatching("href", "[invalid");
// after
String regex = "^https?://";
Pattern.compile(regex); // validate early
doc.getElementsByAttributeValueMatching("href", regex);
Defensive patterns

Strategy: validation

Validate before calling

boolean validRegex(String r) { try { java.util.regex.Pattern.compile(r); return true; } catch (Exception e) { return false; } }

Try / catch

try { doc.getElementsByAttributeValueMatching(key, regex); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith("Pattern syntax error")) { /* report invalid regex */ } throw e; }

Prevention

When it happens

Trigger: Calling element.getElementsByAttributeValueMatching("href", "[unclosed") or any regex with unbalanced brackets/parens, dangling quantifiers, or invalid escape sequences.

Common situations: Regexes built from user input or config strings, regexes copied from other regex flavors (e.g. PCRE constructs Java rejects), or string-escaped patterns where backslashes were lost.

Related errors


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

Appendix: source

Thrown at src/main/java/org/jsoup/nodes/Element.java:1418

     * @param pattern compiled regular expression to match against attribute values
     * @return elements that have attributes matching this regular expression
     */
    public Elements getElementsByAttributeValueMatching(String key, Pattern pattern) {
        return Collector.collect(new Evaluator.AttributeWithValueMatching(key, pattern), this);
    }

    /**
     * Find elements that have attributes whose values match the supplied regular expression.
     * @param key name of the attribute
     * @param regex regular expression to match against attribute values. You can use <a href="http://java.sun.com/docs/books/tutorial/essential/regex/pattern.html#embedded">embedded flags</a> (such as {@code (?i)} and {@code (?m)}) to control regex options.
     * @return elements that have attributes matching this regular expression
     */
    public Elements getElementsByAttributeValueMatching(String key, String regex) {
        Regex pattern;
        try {
            pattern = Regex.compile(regex);
        } catch (PatternSyntaxException e) {
            throw new IllegalArgumentException("Pattern syntax error: " + regex, e);
        }
        return Collector.collect(new Evaluator.AttributeWithValueMatching(key, pattern), this);
    }

    /**
     * Find elements whose sibling index is less than the supplied index.
     * @param index 0-based index
     * @return elements less than index
     */
    public Elements getElementsByIndexLessThan(int index) {
        return Collector.collect(new Evaluator.IndexLessThan(index), this);
    }

    /**
     * Find elements whose sibling index is greater than the supplied index.
     * @param index 0-based index
     * @return elements greater than index
     */

View on GitHub (pinned to 9851ac5d9c)