replace_regex function replaces all matches of a regular expression pattern with another string. This function is an alias for replace and provides the same functionality for regex-based text replacement.
For users of other query languages
If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.Splunk SPL users
Splunk SPL users
In Splunk SPL, you use
rex with mode=sed for regex replacements. APL’s replace_regex provides the same functionality with simpler syntax.ANSI SQL users
ANSI SQL users
In ANSI SQL, you use
REGEXP_REPLACE for regex replacements. APL’s replace_regex provides similar functionality with consistent syntax.Usage
Syntax
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| regex | string | Yes | The regular expression pattern to search for. Can include capture groups. |
| rewrite | string | Yes | The replacement string. Use 1 for the first capture group, etc. |
| text | string | Yes | The source string to perform replacements on. |
Returns
Returns the text with all regex matches replaced by the rewrite pattern. Non-overlapping matches.Use case examples
- Log analysis
- OpenTelemetry traces
- Security logs
Standardize HTTP status codes by adding descriptive prefixes for better readability.QueryRun in PlaygroundOutput
This query adds descriptive prefixes to status codes using regex capture groups, making log analysis more intuitive.
| formatted_status | request_count |
|---|---|
| SUCCESS-200 | 8765 |
| CLIENT_ERROR-404 | 2341 |
| SERVER_ERROR-500 | 1234 |
| CLIENT_ERROR-403 | 987 |
List of related functions
- replace: Alias for replace_regex. Use either name based on preference.
- replace_string: Replaces plain string matches without regex. Use this for faster replacement when regex patterns are not needed.
- extract: Extracts the first regex match. Use this when you need to capture text rather than modify it.
- extract_all: Extracts all regex matches. Use this when you need multiple captured values without replacement.