{"record":{"id":"7fc47c3242197eec","repo":"hashicorp/nomad","slug":"regular-expression-failed-to-compile-v","errorCode":null,"errorMessage":"Regular expression failed to compile: %v","messagePattern":"Regular expression failed to compile: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/structs.go","lineNumber":10074,"sourceCode":"\tif c.Operand == \"\" {\n\t\tmErr.Errors = append(mErr.Errors, errors.New(\"Missing constraint operand\"))\n\t}\n\n\t// requireLtarget specifies whether the constraint requires an LTarget to be\n\t// provided.\n\trequireLtarget := true\n\n\t// Perform additional validation based on operand\n\tswitch c.Operand {\n\tcase ConstraintDistinctHosts:\n\t\trequireLtarget = false\n\tcase ConstraintSetContainsAll, ConstraintSetContainsAny, ConstraintSetContains:\n\t\tif c.RTarget == \"\" {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Set contains constraint requires an RTarget\"))\n\t\t}\n\tcase ConstraintRegex:\n\t\tif _, err := regexp.Compile(c.RTarget); err != nil {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Regular expression failed to compile: %v\", err))\n\t\t}\n\tcase ConstraintVersion:\n\t\tif _, err := version.NewConstraint(c.RTarget); err != nil {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Version constraint is invalid: %v\", err))\n\t\t}\n\tcase ConstraintSemver:\n\t\tif _, err := semver.NewConstraint(c.RTarget); err != nil {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Semver constraint is invalid: %v\", err))\n\t\t}\n\tcase ConstraintDistinctProperty:\n\t\t// If a count is set, make sure it is convertible to a uint64\n\t\tif c.RTarget != \"\" {\n\t\t\tcount, err := strconv.ParseUint(c.RTarget, 10, 64)\n\t\t\tif err != nil {\n\t\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Failed to convert RTarget %q to uint64: %v\", c.RTarget, err))\n\t\t\t} else if count < 1 {\n\t\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Distinct Property must have an allowed count of 1 or greater: %d < 1\", count))\n\t\t\t}","sourceCodeStart":10056,"sourceCodeEnd":10092,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/structs.go#L10056-L10092","documentation":"For operand = \"regexp\" the constraint's RTarget must be a valid Go regular expression. regexp.Compile is run at validation time and any compile error is surfaced as 'Regular expression failed to compile: %v'.","triggerScenarios":"constraint { operand = \"regexp\", value = \"web[0-9\" } — unbalanced brackets, trailing backslash, invalid repetition like '{2,1}', or unsupported syntax in the rtarget/value.","commonSituations":"Hand-written patterns with escaping mistakes (Windows-style backslashes); patterns copied from grep/PCRE with Go-unsupported constructs; interpolation producing malformed patterns.","solutions":["Test the pattern with Go's regexp (RE2) syntax, e.g. go run with regexp.Compile, before submitting.","Fix common syntax errors: balance [], (), escape backslashes, avoid lookahead/lookbehind (unsupported in RE2).","Simplify the pattern or use a version/semver constraint instead of regex where possible."],"exampleFix":"// before\nconstraint {\n  operand = \"regexp\"\n  attribute = \"${node.datacenter}\"\n  value     = \"dc[1-2(\"\n}\n// after\nconstraint {\n  operand = \"regexp\"\n  attribute = \"${node.datacenter}\"\n  value     = \"dc[1-2]\"\n}","handlingStrategy":"validation","validationCode":"if _, err := regexp.Compile(value); err != nil {\n    return fmt.Errorf(\"constraint regex invalid: %v\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Precompile patterns with Go regexp (RE2) before shipping jobs","Avoid PCRE-only features (lookaheads, backrefs)","Run nomad job validate in CI to catch regex errors early"],"tags":["nomad","constraint","regex","validation"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}