Regex Replacement
Apply user-defined regular expression substitution rules to requests and LLM responses.
Regex Replacement applies an ordered list of user-defined regular expression substitution rules to request messages and/or LLM responses. Rules are applied sequentially — the output of one rule is the input of the next — giving you fine-grained control over text patterns at the gateway level.
Use this module to mask, reformat, or remove any pattern-matched text without modifying your application code.
Stages
| Stage | Behavior |
|---|---|
| PRE | Rules are applied to all user messages before the request is forwarded to the LLM |
| POST | Rules are applied to the full LLM response before it is returned to the client |
| PRE + POST | Rules run in both directions (useful for symmetric masking) |
Configuration
Replacement Rules
An ordered list of regex substitution rules. Each rule is applied to the text in sequence.
Each rule has the following fields:
| Field | Required | Description |
|---|---|---|
| Rule Name | Yes | A short label describing what the rule does (e.g., Redact SSN) |
| Regex Pattern | Yes | A valid Python re module regular expression |
| Replacement | Yes | Substitution string. Backreferences (\1, \2, etc.) are supported |
| Ignore Case | No | When enabled, matching is case-insensitive (default: false) |
Python regex syntax
Patterns follow Python re module syntax. Common constructs:
\d— any digit\w— any word character(?i)— inline case-insensitive flag\1,\2— backreferences to capture groups
Rules with invalid patterns are skipped and a warning is logged — they do not cause the request to fail.
Examples
Redact US Social Security Numbers
| Field | Value |
|---|---|
| Pattern | \d{3}-\d{2}-\d{4} |
| Replacement | [SSN] |
| Ignore Case | No |
Before: My SSN is 123-45-6789
After: My SSN is [SSN]
Normalize phone numbers to E.164 format
| Field | Value |
|---|---|
| Pattern | \((\d{3})\)\s*(\d{3})-(\d{4}) |
| Replacement | +1\1\2\3 |
| Ignore Case | No |
Before: Call me at (415) 555-0123
After: Call me at +14155550123
Strip internal code comments from prompts
| Field | Value |
|---|---|
| Pattern | //.* |
| Replacement | `` (empty) |
| Ignore Case | No |
Before: const x = 1; // internal counter
After: const x = 1;
Execution Order
Rules execute in the order they appear in the list. This means:
- Rule 1 processes the original text → produces
output_1 - Rule 2 processes
output_1→ producesoutput_2 - …and so on
Order matters when rules could interact. For example, if Rule 1 removes a delimiter that Rule 2 relies on, Rule 2 may not match as expected.
Example Use Cases
- Compliance masking: Redact specific data patterns (SSNs, account numbers) that NLP-based PII detection may miss.
- Format normalization: Standardize data formats (phone numbers, dates) before they reach the LLM.
- Prompt hygiene: Strip internal debug comments or metadata from prompts that shouldn't be seen by the provider.
- Output shaping: Remove unwanted boilerplate phrases or formatting artifacts from LLM responses.