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#
| Level | Supported | Notes |
|---|---|---|
| Span | Yes | Evaluate any span output against regex patterns. |
Input Requirements#
The MatchesRegex evaluator requires one input:
| Field | Type | Description |
|---|---|---|
output | string | The text to evaluate against the regex pattern |
Constructor Arguments#
| Argument | Type | Description |
|---|---|---|
pattern | str or Pattern | The regex pattern (string or compiled) |
name | str (optional) | Custom evaluator name (default: “matches_regex”) |
include_explanation | bool (optional) | Include match details in explanation (default: True) |
Output Interpretation#
The evaluator returns a Score object with the following properties:
| Property | Value | Description |
|---|---|---|
score | 1.0 or 0.0 | 1.0 if pattern matches, 0.0 if no match |
explanation | string | Number of matches found or “no match” message |
kind | "code" | Indicates this is a code-based evaluator |
direction | "maximize" | Higher scores are better |
Usage Examples#
The MatchesRegex evaluator is not available as a built-in for TypeScript. You can create an equivalent code evaluator using createEvaluator:
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#
- Python: MatchesRegex
Related#
- Exact Match Evaluator - For exact string comparison

