Use matches regex conditions in GA4

How GA4's matches regex condition works, where it changes only a report view and where it affects collected data, and when a simpler condition is the better choice.

Google Analytics 4 lets you match dimension values with a regular expression instead of a fixed string. One pattern can group many page paths, campaign sources or hostnames. A pattern can also quietly match too much or too little. This review explains what the condition does, where it changes stored data rather than only what you see, and when a plain condition is the safer choice.

Is a regex condition the right tool for your task?

Use a regex condition when you need to match several values that share a structure and no single built-in condition can express them. Examples are a set of URL sections, spelling variants of a traffic source, or a family of event names. The main benefit is that one rule covers many values. The main tradeoff is that a regex is harder to read and to review, and a small mistake can change which data is included without any visible error.

Use a plain condition such as exactly matches, contains or begins with when it does the job. A plain condition is easier for teammates to understand, less likely to be mistyped, and simpler to audit later. This matters most in settings that alter or exclude incoming data, where a mistake is costly.

How the condition evaluates values

Google documents that GA4 uses the RE2 regular expression syntax. RE2 supports the usual building blocks:
• Character classes
• Quantifiers
• Alternation with the pipe character
• Grouping
• Anchors
RE2 deliberately omits some features found in other regex flavors, notably lookahead, lookbehind and backreferences. A pattern that works in a JavaScript or PCRE tester can therefore be rejected by GA4 or behave differently there.

The second point is match scope. In many GA4 interfaces, matches regex requires the pattern to match the entire value, not just part of it. Universal Analytics behaved differently, and that difference causes a lot of confusion. Some interfaces also offer a separate partially matches regex option, which succeeds if the pattern matches anywhere in the value. Which options appear, and how they behave, depends on where you are building the condition. Check the operator label and test the result rather than assuming.

Condition | What it checks | Typical use
exactly matches | The whole value equals the string | One known page or source
contains | The string appears anywhere in the value | A keyword in a URL
begins with | The value starts with the string | A URL section
matches regex | The pattern matches the whole value, in many GA4 interfaces | Several structured values in one rule
partially matches regex, where offered | The pattern matches some part of the value | Patterns that should match inside a longer value

Under full-match behavior, a pattern that would contain a fragment elsewhere has to account for the rest of the value. To match any page path within two sections, the pattern must cover the characters after the section name:

^/(blog|news)/.*

With full matching, the anchors in that pattern are redundant but harmless, and they make the intent clear to anyone reading it. Without the trailing .* the pattern would match only values that end exactly at the section name, which is rarely what you want.

Alternation is often used to group exact variants. Under full matching, this pattern matches only the listed values, not any longer value that contains them:

facebook|fb|m\.facebook\.com|l\.facebook\.com

The dots are escaped because an unescaped dot matches any character. Matching is case-sensitive by default. RE2 supports an inline (?i) flag for case-insensitive matching, but confirm that the specific GA4 field accepts it before relying on it. Listing the case variants you expect is the more transparent option.

What changes: the report view or the collected data

The same regex can have very different consequences depending on where you use it. Before saving a pattern, decide which of these categories you are working in.

  • View-only: Filters and comparisons in standard reports, and filters or segments in Explorations, change what is displayed. They do not alter stored data, and you can edit or remove them freely.
  • Definitions applied to reporting: Audience conditions and custom channel group rules decide how users or sessions are classified. Their scope, including how they treat historical data, varies by feature, so check the documentation for the specific feature before you rely on the results.
  • Changes to incoming data: Conditions used to create or modify events change data as it is collected. They do not rewrite past data.
  • Permanent exclusions: Internal traffic and other data filters that are active exclude matching data, and Google states that the excluded data cannot be recovered.

For the last two categories, test the pattern in a view-only context first, such as an Exploration filter on recent data. Where the feature offers a testing state, use it before switching the filter to active.

Choosing the right dimension

A regex can only match the text the dimension actually contains. Page path dimensions and full page URL dimensions hold different strings. One may include the protocol, hostname or query string while another does not. A pattern written for a full URL will fail against a path-only dimension under full matching, because the extra characters are not part of the value. Before writing a pattern, look at sample values of the exact dimension in a report or Exploration.

Copying and sharing patterns

Regex patterns are often copied from documentation, chat messages or spreadsheets. That process can introduce characters that look correct but are not:
• Curly quotes
• Non-breaking spaces
• Zero-width characters
• Trailing spaces
• Dashes converted by a word processor
Any of these can make a pattern match nothing, or match differently, without an obvious error. Paste patterns through a plain-text editor, or retype short ones, and check for stray whitespace at either end.

Readability matters when others maintain the property. A dense pattern is hard to review, and screen readers announce symbol-heavy strings in a way that is hard to follow. Store each pattern alongside a plain-language description of what it should and should not match, plus a few example values. If a plain condition can express the same rule, it is usually the more maintainable choice.

Testing a pattern before you rely on it

  • 1. Use a regex tester set to the RE2 or Go flavor, since Go's standard regular expression package uses RE2 syntax. Testers that default to JavaScript or PCRE may accept features GA4 does not support.
  • 2. Test with full-match behavior in mind. Check that each intended value matches as a whole, not just that a fragment is highlighted.
  • 3. Include negative examples, meaning values that should not match, especially ones that share a prefix or substring with the intended values.
  • 4. Confirm in GA4 itself by applying the pattern as an Exploration filter and comparing the results against known values.
  • 5. Recheck after site or campaign naming changes, because a pattern that was correct can silently stop covering new values.

Gaps in this review

This review is based on general, documented behavior of GA4 and RE2. It does not include verified tests of specific interfaces. It does not state pattern length limits or a complete list of where each regex operator is offered, because those details vary by feature and can change. Check the current GA4 documentation for the feature you are configuring, and treat a pattern as verified only after it has been tested against real values in your property.