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.
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
This is a code-based evaluator using Python’s re module. For exact string matching, use exact_match instead.

Supported Levels

Input Requirements

The MatchesRegex evaluator requires one input:

Constructor Arguments

Output Interpretation

The evaluator returns a Score object with the following properties:

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