extract

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

The extract function retrieves the first substring that matches a regular expression from a source string. Use this function when you need to pull out specific patterns from log messages, URLs, or any text field using regex capture groups.

Usage

Syntax

extract(regex, captureGroup, text)

Parameters

NameTypeRequiredDescription
regexstringYesA regular expression pattern with optional capture groups.
captureGroupintYesThe capture group to extract. Use 0 for the entire match, 1 for the first group, 2 for the second, etc.
textstringYesThe source string to search.

Returns

Returns the substring matched by the specified capture group, or null if no match is found.

Use case examples

Extract user IDs from HTTP request URIs to identify which users are accessing specific endpoints.

Query

['sample-http-logs']
| extend user_id = extract('/users/([0-9]+)', 1, uri)
| where isnotempty(user_id)
| summarize request_count = count() by user_id, method
| sort by request_count desc
| limit 10

Run in Playground

Output

user_idmethodrequest_count
12345GET234
67890POST187
11111GET156
22222PUT98

This query extracts numeric user IDs from URIs like '/users/12345' using a regex capture group, helping analyze per-user API usage patterns.

Extract version numbers from service names to track which service versions are running.

Query

['otel-demo-traces']
| extend version = extract('v([0-9]+[.][0-9]+)', 1, ['service.name'])
| where isnotempty(version)
| summarize span_count = count() by ['service.name'], version
| sort by span_count desc
| limit 10

Run in Playground

Output

service.nameversionspan_count
frontend-v2.12.13456
checkout-v1.51.52341
cart-v3.03.01987

This query extracts version numbers from service names, helping track which versions of services are generating traces.

Extract IP addresses from URIs or request headers to identify the source of suspicious requests.

Query

['sample-http-logs']
| extend ip_address = extract('([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3})', 1, uri)
| where status == '403' or status == '401'
| where isnotempty(ip_address)
| summarize failed_attempts = count() by ip_address, status
| sort by failed_attempts desc
| limit 10

Run in Playground

Output

ip_addressstatusfailed_attempts
192.168.1.10040145
10.0.0.2540332
172.16.0.5040128

This query extracts IP addresses embedded in URIs from failed authentication requests, helping identify potential attackers or misconfigured systems.

  • extract_all: Extracts all matches of a regex pattern. Use this when you need multiple matches instead of just the first one.
  • parse_json: Parses JSON strings into dynamic objects. Use this when working with structured JSON data rather than regex patterns.
  • split: Splits strings by a delimiter. Use this for simpler tokenization without regex complexity.
  • replace_regex: Replaces regex matches with new text. Use this when you need to modify matched patterns rather than extract them.

Other query languages