Regex Replace
Synopsis
A text processing processor that finds and replaces text patterns using regular expressions, for data cleaning, formatting, and normalization.
Schema
- regex_replace:
field: <ident>
regex: <string>
replacement: <string>
target_field: <ident>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>
Configuration
The following fields are used to define the processor:
| Field | Required | Default | Description |
|---|---|---|---|
field | Y | - | Field containing the text to process |
regex | Y | - | Regular expression pattern to match |
replacement | Y | - | Replacement text or pattern |
target_field | N | field | Field to store the modified text |
description | N | - | Explanatory note |
if | N | - | Condition to run |
ignore_failure | N | false | Continue if regex processing fails |
ignore_missing | N | false | Continue if source field doesn't exist |
on_failure | N | - | See Handling Failures |
on_success | N | - | See Handling Success |
tag | N | - | Identifier |
disabled | N | false | When true, the processor is skipped and the event continues to the next one. Lets you take a processor out of the path without removing its configuration |
Details
The processor uses regular expressions to find and replace text patterns within string fields. It supports both simple text replacement and complex pattern matching with capture groups and backreferences.
This processor is an alias for the gsub processor, providing the same functionality with a more descriptive name.
Regular expression patterns support character classes, quantifiers, anchors, and grouping. The replacement string can include backreferences ($1, $2, etc.) to captured groups from the regex pattern.
The processor handles all occurrences of the pattern within the text, making it suitable for comprehensive text cleaning and transformation tasks.
Test regex patterns thoroughly to avoid unintended matches or performance issues with complex patterns.
Regular Expression Semantics
Patterns compile with Go's RE2 engine. A pattern RE2 rejects — one using a lookaround or a backreference — is retried on a .NET-compatible backtracking engine, so both syntaxes are accepted. Every match made by that fallback engine is bounded by a 100ms timeout: a pattern that exceeds it fails the record with an error, and the rest of the pipeline continues.
Matching is unanchored and case-sensitive. A plain string matches anywhere in the field — anchor with ^ and $, and prefix the expression with (?i) for case-insensitive matching.
A pattern is rejected before it compiles when it:
- exceeds 1000 characters,
- uses more than 100 quantifiers (
*,+,{n,m}), - nests groups more than 10 levels deep, or
- takes longer than 100ms to compile.
Examples
Basic Text Replacement
Replacing simple text patterns... | |
updates the error level: | |
Pattern Matching with Capture Groups
Using capture groups for reformatting... | |
reformats the date: | |
Email Masking
Masking email addresses for privacy... | |
masks the username portion: | |
Log Level Normalization
Normalizing various log level formats... | |
standardizes log levels: | |
URL Path Extraction
Extracting paths from URLs... | |
extracts just the path: | |
Multi-Pattern Replacement
Applying multiple regex replacements... | |
sanitizes sensitive information: | |