Skip to main content

Documentation Index

Fetch the complete documentation index at: https://arizeai-433a7140.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Compiles a regex and reports a binary match / mismatch against the model’s output — handy for asserting format invariants like phone numbers, currency strings, UUIDs, or identifier prefixes without paying for a model call. For a zero-code option, see the pre-built Regex Match metric. Use the version below when you want to extend the logic — capture groups, multiple alternative patterns, richer explanations.

Code

import re


def evaluate(output, pattern):
    if output is None:
        return {
            "label": "missing",
            "score": 0.0,
            "explanation": "Output is missing.",
        }

    try:
        compiled = re.compile(pattern)
    except re.error as exc:
        return {
            "label": "invalid_pattern",
            "score": 0.0,
            "explanation": f"Failed to compile regex {pattern!r}: {exc}",
        }

    match = compiled.search(str(output))
    if match is None:
        return {
            "label": "mismatch",
            "score": 0.0,
            "explanation": f"Output does not match /{pattern}/.",
        }
    return {
        "label": "match",
        "score": 1.0,
        "explanation": f"Matched substring: {match.group(0)!r}",
    }
Both re.search and String.match find a match anywhere in the string. If you need the entire output to match, switch to re.fullmatch in Python, or anchor the pattern with ^...$ in TypeScript.

Input mapping

ParameterBind to
outputThe model output, usually output.
patternUse a literal regex string (e.g. ^[A-Z]{3}-\d{4}$) when every example shares the same pattern, or a dataset path (e.g. metadata.expected_pattern) when patterns vary per example.

Output configuration

Categorical labels:
LabelScore
match1.0
mismatch0.0
missing0.0
invalid_pattern0.0
Optimization direction: maximize.

Runtime requirements

SettingValue
SandboxAny — works in the in-process WebAssembly (Python) or Deno (TypeScript) backends.
DependenciesNone — uses re / built-in RegExp.
Internet accessNot required.
Environment variablesNone.
Untrusted regex patterns can be vulnerable to catastrophic backtracking (ReDoS). If pattern is sourced from per-example dataset values rather than a fixed literal, prefer patterns from a trusted column you control.