Pattern library / Strings & Text
Regex to Match Any Word from a List
\b(?:error|warn|critical)\b finds whichever word from your list appears in the text. The \b boundaries are what make it safe: error fires only as a whole word, never inside errors or error_rate. Valid RE2, so the same pattern runs in GA4, BigQuery, Go and Rust.
How the pattern works
\b— a word boundary. It matches only at the start or end of a word (letters, digits, underscore), so the pattern never fires inside a longer word.(?:...)— a non-capturing group holding the alternatives; the|inside it reads as plain "or": error, warn or critical.- Keep the whole list inside one group. Bare
error|warn|criticalstill works, but add an anchor and the pipe splits the pattern:^error|warn$means "starts with error" or "ends with warn". - Need plurals too? Make each word optional-plural:
\b(?:errors|warns|criticals)\bor\b(?:error|warn|critical)s?\b.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
timeout: critical failure upstream | Match | critical matches as a whole word | Try → |
everything completed with no issues | No match | none of the listed words appear | Try → |
an error occurred while parsing | Match | error matches between two word boundaries | Try → |
errors were counted in the report | No match | \b blocks the prefix match — errors is not error | Try → |
ERROR: disk full | No match | case-sensitive by default; add the i flag to catch ERROR | Try → |
error_rate exceeded threshold | No match | underscore is a word character, so \b never fires inside error_rate | Try → |
Where to use it
JavaScript
const re = /\b(?:error|warn|critical)\b/g; 'retry failed: critical'.match(re); // ['critical']
Python
import re re.findall(r'\b(?:error|warn|critical)\b', 'an error occurred') # ['error']
BigQuery / GA4 (RE2)
SELECT REGEXP_CONTAINS('retry failed: critical', r'\b(?:error|warn|critical)\b')
Common mistakes
- Case-sensitive by default:
ERRORdoes not matcherror. Add theiflag (/…/iin JS,re.Iin Python, inline(?i)for RE2) or lowercase your data first. - Dropping
\binvites false positives: plainerrorthen matches insideerrors,error_rateand eventerror. - In GA4 the pattern full-matches by default. For "the value must be exactly one of these words" anchor it:
^(?:error|warn|critical)$.
FAQ
How do I match a list of words in a sentence?
Put every word in one group and wrap it in word boundaries: \b(?:error|warn|critical)\b. It finds any of the words anywhere in the text — swap in your own list and load it into the tester to check.
How do I check that a value is exactly one of the list?
Anchor the same group: ^(?:error|warn|critical)$. Without the anchors the pattern matches whenever the word merely appears somewhere inside the string.
Does this work in Google Analytics 4?
Yes — word boundaries, groups and alternation are all RE2 features, so this pattern runs in GA4, BigQuery, Go and Rust. Remember GA4 full-matches by default: drop the anchors for "contains any of these words", keep them for exact values.
Related patterns
Log Level Regex — Match INFO / WARN / ERROR Lines · Country Code Regex - ISO 3166 Alpha-2 / Alpha-3 · Hashtag Regex - Match #tags in Posts & Text
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.