Gsub
Synopsis
Performs pattern-based string replacements using regular expressions.
Schema
- gsub:
field: <ident>
pattern: <string>
replacement: <string>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>
target_field: <ident>
Configuration
The following fields are used to define the processor:
| Field | Required | Default | Description |
|---|---|---|---|
field | Y | - | Source field containing text to modify |
pattern | Y | - | Regular expression pattern to match |
replacement | Y | - | Text or pattern to replace matches with |
description | N | - | Documentation note |
if | N | - | Conditional expression |
ignore_failure | N | false | Skip processing errors |
ignore_missing | N | false | Skip if input field missing |
on_failure | N | - | Error handling processors |
on_success | N | - | Success handling processors |
tag | N | - | Identifier for logging |
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 |
target_field | N | field | Output field for modified text |
Details
The processor supports both simple string replacements and complex regex patterns, with the ability to store results in the original field or a new target field.
The processor caches compiled regular expressions when dealing with multiple documents with the same pattern. Complex patterns are only compiled once and reused for subsequent matches.
Be careful with complex regular expressions on large text fields, as these may impact performance. Test patterns thoroughly, and consider using simpler patterns when possible.
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
Replacing error code in message... | |
modifies original field: | |
Anonymization
Anonymizing IP addresses in logs... | |
replaces all: | |
Keep Original
Storing the modified text in a new field... | |
preserves the original field: | |
Conditionals
Replacing based on criteria... | |
executes the replacement conditionally: | |
Error Handling
Handling missing fields gracefully... | |
continues the execution: | |