{"record":{"id":"30d434a0756b9b45","repo":"jhy/jsoup","slug":"pattern-syntax-error-30d434","errorCode":null,"errorMessage":"Pattern syntax error: ","messagePattern":"Pattern syntax error: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/jsoup/nodes/Element.java","lineNumber":1418,"sourceCode":"     * @param pattern compiled regular expression to match against attribute values\n     * @return elements that have attributes matching this regular expression\n     */\n    public Elements getElementsByAttributeValueMatching(String key, Pattern pattern) {\n        return Collector.collect(new Evaluator.AttributeWithValueMatching(key, pattern), this);\n    }\n\n    /**\n     * Find elements that have attributes whose values match the supplied regular expression.\n     * @param key name of the attribute\n     * @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.\n     * @return elements that have attributes matching this regular expression\n     */\n    public Elements getElementsByAttributeValueMatching(String key, String regex) {\n        Regex pattern;\n        try {\n            pattern = Regex.compile(regex);\n        } catch (PatternSyntaxException e) {\n            throw new IllegalArgumentException(\"Pattern syntax error: \" + regex, e);\n        }\n        return Collector.collect(new Evaluator.AttributeWithValueMatching(key, pattern), this);\n    }\n\n    /**\n     * Find elements whose sibling index is less than the supplied index.\n     * @param index 0-based index\n     * @return elements less than index\n     */\n    public Elements getElementsByIndexLessThan(int index) {\n        return Collector.collect(new Evaluator.IndexLessThan(index), this);\n    }\n\n    /**\n     * Find elements whose sibling index is greater than the supplied index.\n     * @param index 0-based index\n     * @return elements greater than index\n     */","sourceCodeStart":1400,"sourceCodeEnd":1436,"githubUrl":"https://github.com/jhy/jsoup/blob/9851ac5d9c576c6888910b5a51a2362bbc978959/src/main/java/org/jsoup/nodes/Element.java#L1400-L1436","documentation":"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>').","triggerScenarios":"Calling element.getElementsByAttributeValueMatching(\"href\", \"[unclosed\") or any regex with unbalanced brackets/parens, dangling quantifiers, or invalid escape sequences.","commonSituations":"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.","solutions":["Fix the regex syntax; test it with Pattern.compile(regex) in isolation first","Validate the pattern with Pattern.compile in a try-catch before passing it to jsoup","If the source is user input, surface a validation error instead of executing the search","Check escaping: a Java string literal needs double backslashes, e.g. \"\\\\d+\" for \\d+"],"exampleFix":"// before\ndoc.getElementsByAttributeValueMatching(\"href\", \"[invalid\");\n// after\nString regex = \"^https?://\";\nPattern.compile(regex); // validate early\ndoc.getElementsByAttributeValueMatching(\"href\", regex);","handlingStrategy":"validation","validationCode":"boolean validRegex(String r) { try { java.util.regex.Pattern.compile(r); return true; } catch (Exception e) { return false; } }","typeGuard":null,"tryCatchPattern":"try { doc.getElementsByAttributeValueMatching(key, regex); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Pattern syntax error\")) { /* report invalid regex */ } throw e; }","preventionTips":["Compile-test user-supplied regexes before use","Use Pattern.quote for literal text","Double-escape backslashes in Java literals","Restrict patterns to a safe regex subset"],"tags":["regex","validation","dom"],"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"}