countof_regex

This page explains how to use the countof_regex function in APL.

The 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.

Usage

Syntax

countof_regex(regex, text)

Parameters

NameTypeRequiredDescription
regexstringYesThe regular expression pattern to search for within the text.
textstringYesThe source string where pattern occurrences are counted.

Returns

Returns the number of times the regex pattern matches in the text.

Use case examples

Count numeric patterns in URIs to identify parameterized endpoint usage.

Query

['sample-http-logs']
| extend numeric_params = countof_regex('[0-9]+', uri)
| where numeric_params > 0
| summarize avg_params = avg(numeric_params), request_count = count() by method
| sort by request_count desc

Run in Playground

Output

methodavg_paramsrequest_count
GET1.83421
POST1.21876
PUT2.1654
DELETE1.5234

This query counts numeric parameters in request URIs using regex, helping identify how frequently parameterized endpoints are accessed by different HTTP methods.

Count specific character patterns in trace IDs to analyze ID generation patterns.

Query

['otel-demo-traces']
| extend hex_chars = countof_regex('[a-f]', trace_id)
| summarize avg_hex_chars = avg(hex_chars), trace_count = count() by ['service.name']
| sort by trace_count desc
| limit 10

Run in Playground

Output

service.nameavg_hex_charstrace_count
frontend8.32345
checkout8.11987
cart8.51654
product-catalog7.91234

This query counts hexadecimal characters (a-f) in trace IDs to analyze the distribution of characters, which can help identify issues with trace ID generation.

Identify requests with multiple special characters that might indicate injection attacks.

Query

['sample-http-logs']
| extend special_chars = countof_regex('[<>%;()&+]', uri)
| where special_chars >= 3
| project _time, uri, special_chars, id, status, method
| sort by special_chars desc
| limit 10

Run in Playground

Output

_timeurispecial_charsidstatusmethod
2024-11-06T10:00:00Z/search?q=<script>alert('xss')</script>8user123403GET
2024-11-06T10:01:00Z/api?param='OR'1'='16user456403POST

This query counts special characters commonly used in injection attacks, helping identify potentially malicious requests that warrant further investigation.

  • 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.

Other query languages