countof_regex function counts occurrences of a regular expression pattern within a string. Use this function when you need to count complex patterns or character classes in log messages, requiring more flexibility than simple substring matching.
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 max_match to count regex matches. APL’s countof_regex provides a more straightforward approach.ANSI SQL users
ANSI SQL users
In ANSI SQL, counting regex matches typically requires database-specific functions. APL’s
countof_regex provides a standard approach.Usage
Syntax
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | The source string where pattern occurrences are counted. |
| regex | string | Yes | The regular expression pattern to search for within the text. |
Returns
Returns the number of times the regex pattern matches in the text.Use case examples
- Log analysis
- OpenTelemetry traces
- Security logs
Count numeric patterns in URIs to identify parameterized endpoint usage.QueryRun in PlaygroundOutput
This query counts numeric parameters in request URIs using regex, helping identify how frequently parameterized endpoints are accessed by different HTTP methods.
| method | avg_params | request_count |
|---|---|---|
| GET | 1.8 | 3421 |
| POST | 1.2 | 1876 |
| PUT | 2.1 | 654 |
| DELETE | 1.5 | 234 |
List of related functions
- countof: Counts plain substring occurrences. Use this when you need exact string matching without regex complexity.
- extract: Extracts the first substring matching a regex. Use this when you need to capture the matched text, not just count occurrences.
- extract_all: Extracts all substrings matching a regex. Use this when you need both the count and the actual matched values.
- replace_regex: Replaces all regex matches with another string. Use this when you need to modify matched patterns rather than count them.