Runtime ModulesTransformation

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

StageBehavior
PRERules are applied to all user messages before the request is forwarded to the LLM
POSTRules are applied to the full LLM response before it is returned to the client
PRE + POSTRules 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:

FieldRequiredDescription
Rule NameYesA short label describing what the rule does (e.g., Redact SSN)
Regex PatternYesA valid Python re module regular expression
ReplacementYesSubstitution string. Backreferences (\1, \2, etc.) are supported
Ignore CaseNoWhen 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

FieldValue
Pattern\d{3}-\d{2}-\d{4}
Replacement[SSN]
Ignore CaseNo

Before: My SSN is 123-45-6789 After: My SSN is [SSN]


Normalize phone numbers to E.164 format

FieldValue
Pattern\((\d{3})\)\s*(\d{3})-(\d{4})
Replacement+1\1\2\3
Ignore CaseNo

Before: Call me at (415) 555-0123 After: Call me at +14155550123


Strip internal code comments from prompts

FieldValue
Pattern//.*
Replacement`` (empty)
Ignore CaseNo

Before: const x = 1; // internal counter After: const x = 1;


Execution Order

Rules execute in the order they appear in the list. This means:

  1. Rule 1 processes the original text → produces output_1
  2. Rule 2 processes output_1 → produces output_2
  3. …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.

On this page