{"record":{"id":"3230b4b601df1f53","repo":"apache/cassandra","slug":"string-s-is-not-a-valid-regular-expression","errorCode":null,"errorMessage":"String '%s' is not a valid regular expression","messagePattern":"String '(.+?)' is not a valid regular expression","errorType":"validation","errorClass":"InvalidConstraintDefinitionException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/constraints/RegexpConstraint.java","lineNumber":95,"sourceCode":"    @Override\n    public List<Operator> getSupportedOperators()\n    {\n        return ALLOWED_FUNCTION_OPERATORS;\n    }\n\n    @Override\n    public void validate(ColumnMetadata columnMetadata, String regexp) throws InvalidConstraintDefinitionException\n    {\n        super.validate(columnMetadata, regexp);\n        try\n        {\n            // compilation of a regexp every single time upon evaluation is not performance friendly\n            // so we \"cache\" the compiled regexp for further reuse upon actual validation\n            pattern = Pattern.compile(ParseUtils.unquote(regexp));\n        }\n        catch (Exception e)\n        {\n            throw new InvalidConstraintDefinitionException(format(\"String '%s' is not a valid regular expression\", ParseUtils.unquote(regexp)));\n        }\n    }\n\n    @Override\n    public boolean equals(Object o)\n    {\n        if (this == o)\n            return true;\n\n        if (!(o instanceof RegexpConstraint))\n            return false;\n\n        RegexpConstraint other = (RegexpConstraint) o;\n\n        return columnName.equals(other.columnName);\n    }\n}\n","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/constraints/RegexpConstraint.java#L77-L113","documentation":"Thrown by RegexpConstraint.validate at constraint-definition time when the declared regexp string cannot be compiled by java.util.regex.Pattern. The constraint definition is invalid, so Cassandra rejects the schema change with InvalidConstraintDefinitionException.","triggerScenarios":"CREATE TABLE / ALTER TABLE adding a regexp constraint whose pattern is syntactically invalid (unclosed bracket/parenthesis, dangling escape, invalid quantifier), including patterns corrupted by CQL quoting/escaping.","commonSituations":"Hand-written patterns with unescaped backslashes; double-quoting issues where CQL unquoting eats escape characters; patterns ported from PCRE/JS syntax using Java-unsupported constructs.","solutions":["Test the pattern in Java (Pattern.compile) or a Java-regex tester before adding the constraint.","Escape backslashes properly for CQL string literals (double them or use $$...$$ quoting where available).","Simplify the pattern, removing constructs java.util.regex does not support."],"exampleFix":"// before: CHECK col =~ '^[a-z\\'  -- unbalanced bracket, uncompilable\n// after\nCHECK col =~ '^[a-z]+$'","handlingStrategy":"validation","validationCode":"// Java: compile-check the pattern before issuing the schema change\ntry { java.util.regex.Pattern.compile(patternString); }\ncatch (java.util.regex.PatternSyntaxException e) { throw new IllegalArgumentException(\"bad regexp: \" + e.getDescription()); }","typeGuard":null,"tryCatchPattern":"catch (InvalidConstraintDefinitionException e) { log.error(\"constraint definition rejected: {}\", e.getMessage()); }","preventionTips":["Always Pattern.compile-test patterns before ALTER/CREATE.","Watch CQL string escaping of backslashes.","Avoid regex constructs java.util.regex does not support."],"tags":["cassandra","cql","regexp","invalid-schema-definition"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}