apple/pkl · error

doesNotContainRegexMatch

doesNotContainRegexMatch

Error message

doesNotContainRegexMatch

What it means

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.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java:369

      var charIndex = self.indexOf(pattern);
      if (charIndex == -1) {
        CompilerDirectives.transferToInterpreter();
        throw exceptionBuilder()
            .evalError("doesNotContainLiteralMatch")
            .withProgramValue("String", self)
            .withProgramValue("Pattern", pattern)
            .build();
      }
      return self.codePointCount(0, charIndex);
    }

    @TruffleBoundary
    @Specialization
    protected long eval(String self, VmRegex regex) {
      var matcher = regex.matcher(self);
      if (!matcher.find()) {
        CompilerDirectives.transferToInterpreter();
        throw exceptionBuilder()
            .evalError("doesNotContainRegexMatch")
            .withProgramValue("String", self)
            .withProgramValue("Pattern", regex)
            .build();
      }
      return self.codePointCount(0, matcher.start());
    }
  }

  public abstract static class indexOfOrNull extends ExternalMethod1Node {
    @TruffleBoundary
    @Specialization
    protected Object eval(String self, String pattern) {
      var charIndex = self.indexOf(pattern);
      if (charIndex == -1) {
        return VmNull.withoutDefault();
      }
      return (long) self.codePointCount(0, charIndex);

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Use indexOfOrNull(regex) and handle the null case explicitly.
  2. Pre-validate with s.matches(regex) or s.contains(regex) when you only need existence.
  3. Loosen or make case-insensitive the regex (e.g. (?i) prefix) if formatting may vary.
  4. Log/capture the offending string and pattern (the error includes both) and add a fallback parse path.

Example fix

// before
val idx = s.indexOf(Regex("v\\d+\\.\\d+")) // throws when no version found
// after
val idx = s.indexOfOrNull(Regex("v\\d+\\.\\d+")) ?? 0
Defensive patterns

Strategy: fallback

Validate before calling

// Pkl
if (s.contains(regex)) s.indexOf(regex) else -1

Type guard

function regexMatches(s: String, r: Regex): Boolean = s.contains(r)

Try / catch

try { s.indexOf(r) } catch (e: PklError) { -1 }

Prevention

When it happens

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

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

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/9b59402847d19d39. Report an issue: GitHub.