{"record":{"id":"85c493c0b4d642ca","repo":"code4craft/webmagic","slug":"invalid-regex","errorCode":null,"errorMessage":"invalid regex ","messagePattern":"invalid regex ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"webmagic-core/src/main/java/us/codecraft/webmagic/selector/RegexSelector.java","lineNumber":38,"sourceCode":"\n    private Pattern regex;\n\n    private int group = 1;\n\n    public RegexSelector(String regexStr, int group) {\n        this.compileRegex(regexStr);\n        this.group = group;\n    }\n\n    private void compileRegex(String regexStr) {\n        if (StringUtils.isBlank(regexStr)) {\n            throw new IllegalArgumentException(\"regex must not be empty\");\n        }\n        try {\n            this.regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);\n            this.regexStr = regexStr;\n        } catch (PatternSyntaxException e) {\n            throw new IllegalArgumentException(\"invalid regex \"+regexStr, e);\n        }\n    }\n\n    /**\n     * Create a RegexSelector. When there is no capture group, the value is set to 0 else set to 1.\n     * @param regexStr the regular expression.\n     */\n    public RegexSelector(String regexStr) {\n        this.compileRegex(regexStr);\n        if (regex.matcher(\"\").groupCount() == 0) {\n            this.group = 0;\n        } else {\n            this.group = 1;\n        }\n    }\n\n    @Override\n    public String select(String text) {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-core/src/main/java/us/codecraft/webmagic/selector/RegexSelector.java#L20-L56","documentation":"compileRegex wraps Pattern.compile failures: when the supplied string is not a valid Java regular expression, PatternSyntaxException is rethrown as IllegalArgumentException(\"invalid regex \" + regexStr, e). Note the thrown message plus the cause carries the offending pattern.","triggerScenarios":"new RegexSelector(\"[\") or any syntactically invalid pattern (unclosed group, dangling quantifier, bad escape); also ReplaceSelector misuse aside, this is RegexSelector's path.","commonSituations":"Patterns written for another flavor (e.g. PCRE/JS) using unsupported constructs; hand-edited regex with a typo; dynamically built regex where user input breaks syntax.","solutions":["Fix the regex syntax — check the cause PatternSyntaxException message for the position of the error","Test the pattern with Pattern.compile(regex) in isolation or a regex debugger before wiring it in","Escape metacharacters (Pattern.quote) when the pattern is built from user/config text"],"exampleFix":"// before\nnew RegexSelector(\"[(.*?)]\");\n// after\nnew RegexSelector(\"\\\\[(.*?)\\\\]\");","handlingStrategy":"validation","validationCode":"try { java.util.regex.Pattern.compile(regexStr); } catch (PatternSyntaxException e) { /* reject before building selector */ }","typeGuard":null,"tryCatchPattern":"try { sel = new RegexSelector(userRegex); } catch (IllegalArgumentException e) { log.error(\"invalid regex: \" + e.getCause(), e); }","preventionTips":["Unit-test every regex with Pattern.compile","Escape user input with Pattern.quote","Stick to Java regex syntax when porting patterns"],"tags":["webmagic","regex","pattern-syntax","illegal-argument"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"67816a19d68a4fec4657bf1336227e046e251df2","analyzedAt":"2026-09-08T11:15:28.425Z","contentChangedAt":"2026-09-08T11:15:28.425Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}