Skip to main content

Overview#

The MatchesRegex evaluator is a code-based evaluator that checks if the output contains substrings matching a specified regular expression pattern. It’s useful for validating output format, detecting specific content patterns, or checking for required elements.

Note

This evaluator is only available as a built-in for Python. For TypeScript, see the usage example below showing how to create an equivalent evaluator using createEvaluator.

When to Use#

Use the MatchesRegex evaluator when you need to:

  • Validate output format - Check that responses follow expected patterns (URLs, emails, dates)
  • Detect specific content - Find phone numbers, IDs, or other structured data in outputs
  • Enforce formatting rules - Verify outputs contain required elements
  • Pattern-based quality checks - Check for presence of citations, code blocks, or other patterns
Info

This is a code-based evaluator using Python’s re module. For exact string matching, use exact_match instead.

Supported Levels#

LevelSupportedNotes
SpanYesEvaluate any span output against regex patterns.

Input Requirements#

The MatchesRegex evaluator requires one input:

FieldTypeDescription
outputstringThe text to evaluate against the regex pattern

Constructor Arguments#

ArgumentTypeDescription
patternstr or PatternThe regex pattern (string or compiled)
namestr (optional)Custom evaluator name (default: “matches_regex”)
include_explanationbool (optional)Include match details in explanation (default: True)

Output Interpretation#

The evaluator returns a Score object with the following properties:

PropertyValueDescription
score1.0 or 0.01.0 if pattern matches, 0.0 if no match
explanationstringNumber of matches found or “no match” message
kind"code"Indicates this is a code-based evaluator
direction"maximize"Higher scores are better

Usage Examples#

Common Pattern Examples#

Using String Patterns#

You can pass patterns as strings instead of compiled regex:

Using with Phoenix#

Evaluating Traces#

Run evaluations on traces collected in Phoenix and log results as annotations:

Running Experiments#

Use the MatchesRegex evaluator in Phoenix experiments:

API Reference#