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.1is the first pair of parentheses,2the second;0is 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
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.
SigninLogs
| extend Domain = extract(@"@(.+)$", 1, UserPrincipalName)
| summarize Users = dcount(UserPrincipalName) by DomainSecurityEvent
| where EventID == 4624
| extend Name = extract(@"\\(.+)$", 1, Account)
| summarize Logons = count() by Name
| top 5 by LogonsPractice it
None of the free exercises needs this one yet.
+ 1 in the full SC-200 set · all exercises