extract()

extract(regex, group, text) runs a regular expression on text and returns what one capture group matched:

| extend Port = extract(@":(\d+)$", 1, Endpoint)
  • regex: the pattern, best written as a verbatim string @"...", so backslashes stay as they are.
  • group: which group to return. 1 is the first pair of parentheses, 2 the second; 0 is the whole match.
  • text: the string to search.

If the pattern does not match, extract() returns an empty string. Check for that with isnotempty() when a missing value matters.

The result is a string. If you need a number, real Kusto lets you add a type as a fourth argument (typeof(long)); this site does not support that yet.

Typical uses

  • Parameters out of a command line: a script path, a URL, an encoded payload.
  • Parts of identifiers: the domain of an email address, the user folder in a file path.
  • Codes inside messages: an error number in a description.

The regular expression syntax is the same as for matches regex (RE2). To cut a string at a fixed character instead, split() is simpler.

Examples

1 The user folder from a program's path
DeviceProcessEvents
| where FileName == "OneDrive.exe" and DeviceName startswith "lt-gb"
| extend Profile = extract(@"\\Users\\([^\\]+)\\", 1, FolderPath)
| distinct DeviceName, Profile

([^\\]+) is capture group 1: everything up to the next backslash.

2 The domain part of a user name
SigninLogs
| extend Domain = extract(@"@(.+)$", 1, UserPrincipalName)
| summarize Users = dcount(UserPrincipalName) by Domain
3 The account name without the domain
SecurityEvent
| where EventID == 4624
| extend Name = extract(@"\\(.+)$", 1, Account)
| summarize Logons = count() by Name
| top 5 by Logons

Practice it

None of the free exercises needs this one yet.