trim_regex function removes all leading and trailing matches of a regular expression pattern from a string. Use this function to clean strings from both ends using pattern matching, normalize data with complex prefix/suffix patterns, or prepare strings for consistent analysis.
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 multiple
rex commands for bidirectional trimming. APL’s trim_regex handles both ends in one operation.ANSI SQL users
ANSI SQL users
In ANSI SQL, bidirectional regex trimming requires nested functions. APL’s
trim_regex provides a single-function solution.Usage
Syntax
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| regex | string | Yes | The regular expression pattern to remove from both ends. |
| text | string | Yes | The source string to trim. |
Returns
Returns the source string with leading and trailing regex matches removed.Use case examples
- Log analysis
- OpenTelemetry traces
- Security logs
Remove leading and trailing slashes or special characters from URIs.QueryRun in PlaygroundOutput
This query removes leading and trailing slashes from URIs, normalizing paths for consistent endpoint analysis.
| cleaned_uri | method | request_count |
|---|---|---|
| api/users | GET | 2341 |
| api/orders | POST | 1987 |
List of related functions
- trim: Removes leading and trailing characters. Use this for simple character-based trimming without regex.
- trim_start_regex: Removes leading regex matches. Use this for pattern trimming only from the start.
- trim_end_regex: Removes trailing regex matches. Use this for pattern trimming only from the end.
- replace_regex: Replaces regex matches. Use this when you need to replace patterns anywhere, not just trim ends.