matches regex

matches regex is true when a string matches a regular expression:

| where CommandLine matches regex @"-[eE][nN][cC]\s"

The pattern can match anywhere in the string. To match the whole value, anchor it with ^ at the start and $ at the end.

Syntax

Kusto uses the RE2 regular expression syntax: the usual character classes (\d, \w, \s, [a-z]), quantifiers (+, *, ?, {2,4}), groups and alternation (a|b). RE2 has no look-ahead or look-behind.

Write the pattern as a verbatim string, @"...". Otherwise every backslash must be doubled: "\\d+" instead of @"\d+".

Case

matches regex is case-sensitive. Start the pattern with (?i) to ignore case.

When to use it

Regular expressions are powerful but slow on large tables and easy to get subtly wrong. Prefer has, startswith or in when they can express the condition, and filter with them first. Then use matches regex on the rows that are left.

To pull a part of the text out of a string, rather than test it, use extract().

Examples

1 Internal IP addresses in one subnet
DeviceNetworkEvents
| where RemoteIP matches regex @"^10\.10\.0\.\d+$"
| summarize Connections = count() by RemoteIP, RemotePort

^ and $ anchor the pattern to the whole value; \. is a literal dot.

2 Case-insensitive pattern with (?i)
DeviceProcessEvents
| where ProcessCommandLine matches regex @"(?i)-executionpolicy\s+bypass"
| summarize count() by FileName
3 User names that follow the first.last pattern
SigninLogs
| where UserPrincipalName matches regex @"^[a-z]+\.[a-z]+@"
| summarize Users = dcount(UserPrincipalName)

Practice it

None of the free exercises needs this one yet.