{"record":{"id":"39e335bc91def2f5","repo":"code4craft/webmagic","slug":"invalid-regex-39e335","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/ReplaceSelector.java","lineNumber":28,"sourceCode":" *\n * @author code4crafter@gmail.com <br>\n * @since 0.1.0\n */\npublic class ReplaceSelector implements Selector {\n\n    private String regexStr;\n\n    private String replacement;\n\n    private Pattern regex;\n\n    public ReplaceSelector(String regexStr, String replacement) {\n        this.regexStr = regexStr;\n        this.replacement = replacement;\n        try {\n            regex = Pattern.compile(regexStr);\n        } catch (PatternSyntaxException e) {\n            throw new IllegalArgumentException(\"invalid regex\", e);\n        }\n    }\n\n    @Override\n    public String select(String text) {\n        Matcher matcher = regex.matcher(text);\n        return matcher.replaceAll(replacement);\n    }\n\n    @Override\n    public List<String> selectList(String text) {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override\n    public String toString() {\n        return regexStr + \"_\" + replacement;\n    }","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-core/src/main/java/us/codecraft/webmagic/selector/ReplaceSelector.java#L10-L46","documentation":"ReplaceSelector compiles its regexStr in the constructor and rethrows PatternSyntaxException as IllegalArgumentException(\"invalid regex\", e). Unlike RegexSelector, the message does not embed the pattern, so inspect the cause for details.","triggerScenarios":"new ReplaceSelector(badRegex, replacement) where badRegex is not a valid Java regex — e.g. unbalanced parentheses, invalid escape like '\\q', or trailing backslash.","commonSituations":"Replacing text in extracted values with a regex ported from another language; config-provided replacement patterns with typos; Windows path strings used unescaped as patterns (backslashes).","solutions":["Correct the regex syntax; read the chained PatternSyntaxException cause for the error index","Validate with Pattern.compile(regexStr) in a test before constructing the selector","Use Pattern.quote() or double backslashes for literals containing metacharacters/backslashes"],"exampleFix":"// before\nnew ReplaceSelector(\"C:\\\\users\\\\(.*?)\", \"\");\n// after\nnew ReplaceSelector(Pattern.quote(\"C:\\\\users\\\\\") + \"(.*?)\", \"\");","handlingStrategy":"try-catch","validationCode":"try { java.util.regex.Pattern.compile(regexStr); } catch (PatternSyntaxException e) { /* invalid */ }","typeGuard":null,"tryCatchPattern":"try { sel = new ReplaceSelector(regexStr, repl); } catch (IllegalArgumentException e) { throw new ConfigException(\"bad replace pattern\", e.getCause()); }","preventionTips":["Double-check backslashes and escapes in replacement patterns","Validate patterns in tests before deployment","Log the cause PatternSyntaxException since the message omits the pattern"],"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"}