{"record":{"id":"9b59402847d19d39","repo":"apple/pkl","slug":"doesnotcontainregexmatch","errorCode":"doesNotContainRegexMatch","errorMessage":"doesNotContainRegexMatch","messagePattern":"doesNotContainRegexMatch","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java","lineNumber":369,"sourceCode":"      var charIndex = self.indexOf(pattern);\n      if (charIndex == -1) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder()\n            .evalError(\"doesNotContainLiteralMatch\")\n            .withProgramValue(\"String\", self)\n            .withProgramValue(\"Pattern\", pattern)\n            .build();\n      }\n      return self.codePointCount(0, charIndex);\n    }\n\n    @TruffleBoundary\n    @Specialization\n    protected long eval(String self, VmRegex regex) {\n      var matcher = regex.matcher(self);\n      if (!matcher.find()) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder()\n            .evalError(\"doesNotContainRegexMatch\")\n            .withProgramValue(\"String\", self)\n            .withProgramValue(\"Pattern\", regex)\n            .build();\n      }\n      return self.codePointCount(0, matcher.start());\n    }\n  }\n\n  public abstract static class indexOfOrNull extends ExternalMethod1Node {\n    @TruffleBoundary\n    @Specialization\n    protected Object eval(String self, String pattern) {\n      var charIndex = self.indexOf(pattern);\n      if (charIndex == -1) {\n        return VmNull.withoutDefault();\n      }\n      return (long) self.codePointCount(0, charIndex);","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java#L351-L387","documentation":"String.indexOf(regex) returns the code-point index of the first match of a VmRegex, and throws doesNotContainRegexMatch when the pattern matches nowhere in the string. Like the literal variant, Pkl treats 'no match' as an error for the plain method; indexOfOrNull is the non-throwing alternative.","triggerScenarios":"Calling `s.indexOf(Regex(...))` where the regex never matches — overly specific patterns, anchors like ^/$ that don't fit the input, wrong capture groups, or a regex built from interpolated values that no longer fit the data shape.","commonSituations":"Extracting version numbers or dates with a pattern that a new input format no longer matches; anchoring mistakes after trimming changes; case-sensitivity differences (regex is case-sensitive by default).","solutions":["Use indexOfOrNull(regex) and handle the null case explicitly.","Pre-validate with s.matches(regex) or s.contains(regex) when you only need existence.","Loosen or make case-insensitive the regex (e.g. (?i) prefix) if formatting may vary.","Log/capture the offending string and pattern (the error includes both) and add a fallback parse path."],"exampleFix":"// before\nval idx = s.indexOf(Regex(\"v\\\\d+\\\\.\\\\d+\")) // throws when no version found\n// after\nval idx = s.indexOfOrNull(Regex(\"v\\\\d+\\\\.\\\\d+\")) ?? 0","handlingStrategy":"fallback","validationCode":"// Pkl\nif (s.contains(regex)) s.indexOf(regex) else -1","typeGuard":"function regexMatches(s: String, r: Regex): Boolean = s.contains(r)","tryCatchPattern":"try { s.indexOf(r) } catch (e: PklError) { -1 }","preventionTips":["Use indexOfOrNull(regex) and branch on null","Existence-check with contains/matches before indexing","Add (?i) or loosen anchors when format may vary","Keep regex patterns in sync with input format changes"],"tags":["pkl","string","regex","pattern-not-found"],"backgroundTag":"resource-not-found","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}