Regex engines across SIEM platforms
A regular expression is not portable by default. Each product embeds a particular engine, and engines differ in features, speed and small details of meaning. A pattern that works in a tester can fail to compile in production or, worse, compile and match something else.
| Platform | Engine family | Lookaround and backreferences | Notes |
|---|---|---|---|
Splunk (rex, regex) | PCRE | Yes | Backtracking. rex extracts named groups. |
| Microsoft Sentinel (KQL) | RE2-style | No | \d and \w match Unicode. |
| Google SecOps (YARA-L) | RE2-style | No | Use back-quoted strings. |
Elastic (regexp, EQL) | Lucene | No | No anchors. Matches the whole field. |
| This page | JavaScript | Yes | Runs in a worker with a time limit. |
Why RE2 leaves out lookarounds and backreferences
PCRE and JavaScript engines backtrack. At each choice they try one option and return to try another if the rest fails. This supports powerful features, but the number of paths can grow exponentially with the length of the text.
RE2 takes a different approach. It simulates every possibility at once, in the manner of a finite automaton, and reads each character a bounded number of times. Matching time is proportional to the length of the input, whatever the pattern. A cloud service that runs untrusted queries from many customers needs exactly that promise, because one bad pattern could otherwise consume a whole node.
The price is features. A backreference such as (\w+) \1 requires remembering arbitrary text, which
a finite automaton cannot do, and no efficient general algorithm is known. Lookaround does not fit the
single-pass design. RE2 also rejects repeat counts above 1,000.
Rewriting for RE2
- Instead of
(?<=user=)\w+, capture what you need:user=(\w+). - Instead of
^(?!.*healthcheck).*error, use two conditions in the query: matcherrorand nothealthcheck. - Instead of
(\w+)@\1\.com, capture both parts and compare the fields. - Instead of a lookahead that checks a boundary, use
\bor match the delimiter and leave it out of the group.
Catastrophic backtracking in practice
Three structures cause most incidents, and the checker looks for each of them:
| Structure | Risky | Safer |
|---|---|---|
| Repeat inside a repeat | (\w+\s?)+ | \w+(?:\s\w+)* |
| Overlapping alternatives | (\d|\w)+ | \w+ |
| Neighboring repeats | \s*.* | .* |
A required separator makes nesting safe. In (?:\d+\.)+ the dot cannot be matched by \d, so there is
only one way to divide the text. Attackers control much of what appears in logs, such as user agents and URLs, so a
vulnerable pattern in a detection pipeline is a denial-of-service target.
Named capture groups for log parsing
Numbered groups shift when someone adds a parenthesis. A named group documents itself and keeps its meaning:
^(?<ip>\S+) \S+ (?<user>\S+) \[(?<time>[^\]]+)\] "(?<method>[A-Z]+) (?<path>\S+) - JavaScript writes
(?<name>...). PCRE, RE2 and KQL accept that and(?P<name>...). - Splunk turns each named group into a field. Use letters, digits and underscores only.
- KQL
extract()selects a group by number. The snippet counts them for you and names each column. - Use
(?:...)for grouping that you do not need to extract. - Prefer a negated class such as
[^\]]+to.*?. It is faster and states where the field ends.
Performance in high-volume SIEM queries
- Filter before you match. Limit the index, source type and time range first. A regex cannot use an index.
- Use plain terms when you can. In KQL,
hasuses the term index, whilematches regexscans. In Splunk, put literal words before the first pipe. - Anchor the pattern.
^lets the engine give up quickly on lines that cannot match. - Be specific. Replace
.*with the characters you expect, and put an upper limit on repeats. - Extract once. If several searches need a field, parse it at ingest instead of in each query.
- Test near misses. Slow behavior appears on long lines that almost match, and not on lines that match.
About the library and snippets
The ten library patterns avoid lookaround and backreferences, so they compile in RE2-style engines. They are starting points and not complete detections. The IPv4 pattern also matches version numbers, a 32-character hex string may be NTLM instead of MD5, and encoded PowerShell has legitimate uses in management software. Samples use documentation addresses, and the AWS key is the example that Amazon publishes.
Snippets escape the pattern for each language. Splunk strings treat \" and \\ specially. KQL verbatim
strings double the quote. YARA-L back-quoted strings have no escapes. The Elastic snippet rewrites the pattern for Lucene by
expanding shorthand classes, removing anchors and adding .*, and the panel lists anything lost on the way.
Replace field names such as RawData and message with your own, and run the query on a small time
range before you rely on it.
Does the risk check guarantee that a pattern is safe?
No. It is a static heuristic that looks for nested repeats, overlapping alternatives and neighboring repeats that match the same characters. It can miss unusual cases and can flag patterns that are harmless on your data. The timeout is the real protection on this page. In production, test against long lines that almost match.