Regular Expression (Regex)

A regular expression (regex) is a text pattern that matches a set of values rather than one exact string. Marketers use regex to group, filter, and segment analytics data that would otherwise need dozens of separate conditions. Google Analytics 4 runs regex on Google’s RE2 engine, the same syntax used by BigQuery and Looker Studio.
Why Regex Matters for Marketers
Regex collapses many rules into one. A report covering every campaign that starts with q4_ needs one pattern instead of a separate condition per campaign name.
Three jobs it does well in analytics:
- Grouping: pull
/blog/,/guide/, and/glossary/pages into a single content group. - Filtering: strip internal, staging, or test traffic out of a report.
- Segmenting: build an audience from anyone who viewed any pricing-related page.
Google Analytics applies regex to your data before you touch it. The default rule that classifies a session as Paid Search tests utm_medium against a pattern rather than a fixed list, which is why several medium values you never planned for still land in a paid channel.
Regex Syntax: The Characters That Matter
Nine characters cover almost every analytics use case. Everything else is a variation.
| Character | Meaning | Example |
|---|---|---|
. |
Any single character | b.g matches “bag” and “big” |
* |
Zero or more of the previous item | ab*c matches “ac” and “abbc” |
+ |
One or more of the previous item | ab+c matches “abc” but not “ac” |
? |
Previous item is optional | colou?r matches “color” and “colour” |
^ |
Start of the value | ^q4 matches values beginning with “q4” |
$ |
End of the value | sale$ matches values ending in “sale” |
| |
Or | email|sms matches either word |
() |
Groups items together | (spring|fall)_sale |
[] |
Any one character in the set | [0-9] matches a single digit |
{2,4} |
Between 2 and 4 repeats | [0-9]{4} matches a four-digit year |
\ |
Escapes the next character | \. matches a literal period |
The backslash matters more than it looks. A period inside a URL or domain is a wildcard until you escape it, so site.com also matches “sitexcom”.
Where Regex Works in Google Analytics 4
Google’s support documentation lists regex support in data filters, key events, segments, audiences, content groups, and custom channel groups. Dimension filters inside Explorations accept it too.
Standard reports are the exception. The search box above a standard report runs a plain substring search, not a pattern match. To apply a real GA4 regex filter, open an Exploration, add a dimension filter, and pick the regex match type there. That environment is also the fastest place to test a pattern, since it reports the row count immediately.
Regex Match Types in GA4
GA4 offers two regex match types: matches regex and does not match regex. Both evaluate as a full match by default. Google’s documentation states that Analytics regex “is full regex and case sensitive”, meaning the pattern has to match the entire value, not a fragment of it.
That single behavior causes most empty reports. The pattern pricing matches only the exact value “pricing”. To match any page path containing the word, wrap it:
.*pricing.*
Case sensitivity has the same trap. Facebook and facebook are different values. Prefix a pattern with (?i) to ignore case:
(?i)^facebook$
RE2 also drops features common in other regex engines. Lookaheads, lookbehinds, backreferences, and recursion are not supported. RE2 trades them for guaranteed linear-time matching, so a pattern that works in a JavaScript tester can still fail in GA4.
Regex Examples for Campaign Reports
Each pattern below assumes the full-match behavior described above.
Every Q4 campaign, whatever follows the prefix:
q4_.*
Paid social from three networks in one row:
.*(facebook|instagram|linkedin).*
Campaign names ending in a four-digit year:
.*_[0-9]{4}$
Blog pages only, excluding the author archive. RE2 has no negative lookahead, so this takes two conditions rather than one clever pattern. Set “matches regex” to the include pattern:
.*/blog/.*
Then add a second condition set to “does not match regex”:
.*/author/.*
Any medium outside your approved list, which surfaces tagging drift. Use “does not match regex” with:
(?i)^(cpc|email|social|referral|organic)$
Common Regex Mistakes
- Assuming partial matching. GA4 needs
.*on both sides for “contains” behavior. Search Console also uses RE2 but matches partially, so the same pattern behaves differently across the two tools. - Leaving periods unescaped. Write
example\.com, notexample.com. - Ignoring case. Add
(?i)or standardize your tagging. - Porting patterns from PCRE testers. Lookarounds and backreferences silently break in RE2.
- Using regex to fix inconsistent data. A pattern that has to catch “Facebook”, “FB”, and “fb-ads” is a naming problem, not a regex problem. Consistent campaign values cut how much regex you need, and UTM naming rules enforce one spelling before the link ships.
Frequently Asked Questions
What is regex?
Regex is short for regular expression, a pattern that describes a set of text values. Instead of listing every campaign name you want in a report, you write one pattern that matches all of them. Regex is used across analytics tools, spreadsheets, and programming languages, with small syntax differences between them.
Is regex in Google Analytics case sensitive?
Yes. GA4 treats Facebook and facebook as different values by default. Add the (?i) flag at the start of the pattern to match regardless of case. Standardizing your UTM values in lowercase avoids the problem entirely.
What are the GA4 regex match types?
GA4 provides “matches regex” and “does not match regex”. Both apply full matching, so the pattern must describe the whole value. Other match types such as “contains”, “begins with”, and “exactly matches” handle simple cases without a pattern.
Why is my GA4 regex filter returning no data?
Full matching is the usual cause. A pattern like checkout returns nothing unless a value is exactly “checkout”, so change it to .*checkout.*. Check for unescaped periods and case mismatches next, then confirm the pattern does not rely on lookaheads, which RE2 does not support.
To build campaign links with values consistent enough that simple patterns work, use the free UTM builder at linkutm.