apache/cassandra · error · InvalidConstraintDefinitionException

String '%s' is not a valid regular expression

Error message

String '%s' is not a valid regular expression

What it means

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.

Source

Thrown at src/java/org/apache/cassandra/cql3/constraints/RegexpConstraint.java:95

    @Override
    public List<Operator> getSupportedOperators()
    {
        return ALLOWED_FUNCTION_OPERATORS;
    }

    @Override
    public void validate(ColumnMetadata columnMetadata, String regexp) throws InvalidConstraintDefinitionException
    {
        super.validate(columnMetadata, regexp);
        try
        {
            // compilation of a regexp every single time upon evaluation is not performance friendly
            // so we "cache" the compiled regexp for further reuse upon actual validation
            pattern = Pattern.compile(ParseUtils.unquote(regexp));
        }
        catch (Exception e)
        {
            throw new InvalidConstraintDefinitionException(format("String '%s' is not a valid regular expression", ParseUtils.unquote(regexp)));
        }
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
            return true;

        if (!(o instanceof RegexpConstraint))
            return false;

        RegexpConstraint other = (RegexpConstraint) o;

        return columnName.equals(other.columnName);
    }
}

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Test the pattern in Java (Pattern.compile) or a Java-regex tester before adding the constraint.
  2. Escape backslashes properly for CQL string literals (double them or use $$...$$ quoting where available).
  3. Simplify the pattern, removing constructs java.util.regex does not support.

Example fix

// before: CHECK col =~ '^[a-z\'  -- unbalanced bracket, uncompilable
// after
CHECK col =~ '^[a-z]+$'
Defensive patterns

Strategy: validation

Validate before calling

// Java: compile-check the pattern before issuing the schema change
try { java.util.regex.Pattern.compile(patternString); }
catch (java.util.regex.PatternSyntaxException e) { throw new IllegalArgumentException("bad regexp: " + e.getDescription()); }

Try / catch

catch (InvalidConstraintDefinitionException e) { log.error("constraint definition rejected: {}", e.getMessage()); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/3230b4b601df1f53. Report an issue: GitHub.