regex
Keep or drop rows by matching a field against a regular expression. Shorthand for a where regex filter.
Use regex to keep rows whose field matches a regular expression. You can use != to drop matching rows.
Syntax
... | regex <field>="<pattern>"
... | regex <field>!="<pattern>"
... | regex "<pattern>"| Argument | Default | Description |
|---|---|---|
field | message | Field to test. With no field, regex tests message. |
= / != | = keeps matching rows; != drops them. | |
pattern | Regular expression in double quotes (RE2 syntax). |
How it works
regex f="p"is the same filter aswhere f=~"p", andregex f!="p"is the same aswhere f!~"p". See where.- The match is unanchored (it can match anywhere in the value) and case-insensitive. Add
^and$to anchor. regex f!="p"also drops rows wherefis null.- To extract text into new fields, use rex.
regexonly filters.
For a case-sensitive match, use the match() function in where:
class_uid=1007 | where match(process.name, "^PowerShell") | table time, device.hostname, process.nameExamples
Files created with an executable or script extension:
class_uid=1001 | regex file.name="\.(exe|dll|ps1|bat|vbs|js|hta)$" | stats count by file.name, device.hostnamePowerShell download cradles:
class_uid=1007 process.name="powershell.exe" | regex process.cmd_line="invoke-(expression|webrequest)|downloadstring|iex\s*\(" | table time, device.hostname, actor.user.name, process.cmd_lineRequests from clients that aren't browsers:
class_uid=4002 | regex http_request.user_agent!="mozilla" | stats count by http_request.user_agent | sort -countDrop static assets from proxy traffic:
metadata.log_source=conduit_proxy | regex http_request.url.path!="\.(css|js|png|jpg|gif|svg|woff2?)$" | stats count by http_request.url.hostnameTest the event message, the default field:
metadata.log_source=otlp_log | regex "timeout|timed out" | head 20Service accounts that follow a naming pattern:
class_uid=3002 | regex user.name="^svc[-_]" | stats count by user.name, src_endpoint.ipNotes
- Filter with an indexed base-search term first (
class_uid=1007 process.name="powershell.exe"), then applyregexto the smaller set. - A keyword or
field="*text*"search is usually faster than a regex when you only need a substring. regexruns as awherefilter, so it also works after a nano-side stage (lookup,prevalence,inputlookup,resolve_identity,ai).