Free GA4 auditCheck

Regex (regular expression)

Definition

Regex (regular expression) is a pattern of characters that describes what text to look for. Instead of typing dozens of phrases separately, you can write a single rule, e.g. “all URLs under /blog/” or “queries that contain price or cost”. Google Analytics Help describes regex as sequences of characters that broadly or narrowly match patterns in your data.

In SEO and analytics, regex is used mainly to filter reports, segment traffic and define rules. It is supported by Google Search Console, Google Analytics 4 and Google Tag Manager, among others. Each of these tools applies it slightly differently, though.

PatternWhat it matchesExample match
price|costtext with the word “price” or “cost”how much does an seo audit cost
^/blog/URLs starting with /blog//blog/regex-in-seo
\.pdf$URLs ending in .pdf/pricing.pdf
^(how|what|why)\squeries starting with a questionhow does regex work

How is a regular expression built?

A regular expression consists of ordinary characters, which stand for themselves, and metacharacters, which have a special meaning. Letters and digits match literally, while the dot, the asterisk or brackets control the matching. Google Analytics Help lists the basic metacharacters collected in the table.

MetacharacterMeaningExample
.any single characterr.x matches rex and r1x
?the preceding character 0 or 1 timescolou?r matches color and colour
+the preceding character 1 or more timesa+ matches a and aaa
*the preceding character 0 or more times.* matches any string
|alternation, i.e. “or”seo|sem
^start of the string^/blog
$end of the string\.html$
( )group(shoes|footwear) women
[ ]one character from the list[0-9] is any digit
\turns a metacharacter into an ordinary character\. is a literal dot

The RE2 engine used in Google tools also supports character class shorthands. \d means a digit, \s a whitespace character, and their uppercase counterparts \D and \S mean the opposite. Search Console lists these shorthands among the most commonly used patterns.

Why do you need to escape the dot?

In regex, a dot means any character, not a dot. The pattern zest.agency will therefore also match “zestXagency”. To search for a literal dot, you put a backslash in front of it: zest\.agency. The same applies to other metacharacters, e.g. the question mark in a URL with parameters.

Google Analytics Help gives an example of an IP address range: 198\.51\.100\.\d*. The dots are preceded by a backslash here, and \d* means any number of digits at the end.

How do you use regex in Search Console, GA4 and Google Tag Manager?

Regex in Google tools is used to filter data, but each of them matches it differently. Search Console looks for the pattern anywhere in the text and ignores case. Google Analytics 4 requires the whole value to match and is case-sensitive. The same pattern can therefore give different results in the two tools.

ToolDefault matchingCaseTypical use
Google Search Consolepartial, anywhere in the textcase-insensitive, (?-i) turns on case sensitivityquery and page filters
Google Analytics 4full, the whole valuecase-sensitivefilters, segments, audiences, content groups
Google Tag Managerdepends on the chosen conditiondepends on the conditiontag firing rules

Regex in Google Search Console

In Google Search Console, regex works in the Performance report for the Queries and Pages dimensions. The filter is added through the custom regular expression option, “Custom (regex)”. You can choose to match or not match, which lets you exclude branded queries, for example.

Example patterns useful in SEO:

  • ^(how|what|why|when|which)\s – question queries, useful for planning FAQs and how-to content;
  • (price|cost|pricing|how much) – price queries, often with purchase intent;
  • ^https://zest\.agency/blog/ – blog pages only;
  • (zest|zest agency) with the “doesn't match” option – queries without the brand name.

Regex in Google Analytics 4

Google Analytics 4 supports regex in data filters, key events, segments, audiences, content groups and custom channel groups. GA4 Help points out that regex works as a full match by default. The pattern San Francisco will return only that value, while .*San Francisco.* will also return “South San Francisco”. When moving a pattern from Search Console, you therefore need to add .* at the beginning and the end.

Regex in Google Tag Manager

In Google Tag Manager, regex is used to build tag firing rules. A single rule can cover, for example, all thank-you pages or several form URLs. Instead of creating a separate condition for each URL, one pattern with alternation is enough.

How is a regular expression built?

How does RE2 differ from other regex flavours?

RE2 is Google's regular expression engine, which deliberately leaves out some features known from other flavours. The RE2 documentation states that the engine does not support backreferences or lookahead and lookbehind assertions (lookaround). Search Console and GA4 use RE2. Patterns copied from tutorials for other languages may not work there.

FeatureRE2 (Google tools)PCRE (e.g. Apache server)
Basic metacharacters and quantifiersyesyes
Backreferences, e.g. \1noyes
Lookahead and lookbehind, e.g. (?!...)noyes
Running time guaranteelinear in the length of the textnone, can be exponential

Why doesn't RE2 support lookaround?

RE2 gives up these features because they require backtracking. According to the documentation, RE2's main guarantee is matching time that is linear in the length of the text. This lets the engine safely process patterns from untrusted users. In Search Console, the lack of lookahead is worked around with the “Doesn't match regex” filter, i.e. an exclusion, instead of a negation inside the pattern.

What are the most common mistakes?

The most common mistakes come from differences between the tools and from metacharacters used literally:

  • a dot without a backslash in a URL, so the pattern matches more than it should;
  • moving a pattern from Search Console to GA4 without .*, so GA4 returns nothing;
  • a vertical bar at the end of the pattern, which Google Analytics Help warns against;
  • using a lookahead copied from a tutorial for another programming language.

Before deployment, a pattern is best tested on a sample of data. Redirect rules on the server require particular care, because a mistake can affect the whole site. Checking redirects and URL patterns is usually part of an SEO audit.

Frequently asked questions

Q.What does .* mean in regex?

The notation .* means any string of characters, including an empty one. The dot is any single character, and the asterisk lets it repeat any number of times. In Google Analytics 4, .* at the beginning and end of a pattern turns a full match into a partial one. In Search Console it is usually not needed, because matching is partial there by default.

Q.Is regex in Search Console case-sensitive?

No, the regex filter in Search Console is case-insensitive by default. To change this, you add (?-i) at the beginning of the pattern. Search Console Help gives an example: (?-i)AAA matches a URL with “AAA” but not with “aaa”. Google Analytics 4 works the other way round and is case-sensitive by default.

Q.How do you exclude branded queries in Search Console?

Branded queries are excluded with a regex filter using the “Doesn't match regex” option. The pattern contains the brand name and its variants, e.g. misspellings, separated by a vertical bar. The report then shows only traffic from queries without the brand. It is a simple way to assess visibility that does not come from brand recognition.

Q.Why doesn't my regex work in Google Analytics 4?

The most common cause is the full match that GA4 applies by default. The pattern has to describe the whole value, so a URL fragment needs .* added at the beginning and the end. The second cause is often letter case. The third is features not supported by RE2, such as lookahead.

Contact

Let's talk growth

Send a brief or drop your contact. We'll reply within 24h.

Get a quote
Paweł Strzelecki and Karol Majewski, co-founders of Zest