
March 27, 2025
Regex in Google Analytics 4 (Regular Expressions)
Have you opened Google Analytics 4, seen the term “regex” and thought to yourself, “What’s that?” If yes, this article is for you! Even if you have worked with regex (regular expressions) before, there may be something new for you to learn here, too.
Regular expressions are special sequences of characters used to find, extract, replace, or match text, making them powerful tools for text processing tasks in data analysis.
In the context of GA4, you can use regex to filter data in reports and more!
This article will cover the tools you need to build regular expressions, such as basic syntax and patterns, and give some examples of how you can use regex in Google Analytics 4.

Table of Contents
Here’s what you will learn in this article
- What are regular expressions?
- Basic syntax and patterns (with examples)
- Additional tips
- Regular expressions in Google Analytics 4
- Final Thoughts
What are regular expressions?
Regular expressions are combinations of ordinary characters (like letters and digits) and special characters, which have special meanings in the regex syntax (more about that in the next section). They allow you to search for specific patterns or sequences of characters in a string of text.
You will most commonly see “Regex matches” and “Regex does not match” in GA4. “Regex match” means that you will only include data that matches the regex pattern, and “Regex does not match” means that you will include all data except what matches the regex pattern.
Let’s look at an example to help: you have a site that sells shoes and accessories. Each shoe has a details page (/shoes/123-112233). You want to look at the aggregate of page views on these details pages.
You could use the contains filter in GA4, but you can’t quite get this to work since the pattern for these URLS is “/shoes/[3 digits]-[6 digits]”. You can’t just put “/shoes/” since this will include too many pages (perhaps “/shoes/list” exists), and using “/shoes/123” will restrict the data too much. So, what do you do? Use regex!
Before diving into this example, let’s review some basic syntax and patterns you should be familiar with.
Basic syntax and patterns (with examples)
First, we will look at the basic syntax, and later, I will show you where you can use regular expressions in GA4.
Wildcards
Wildcards provide flexibility by allowing for variations or unknowns in the input. There are five key characters that you should be familiar with:
- Period (.): Matches any single character.
- E.g. “/sho.s/” matches “/shops/” or “/shoes/” or “/shows/”
- Question mark (?): Matches the previous character 0 or 1 times.
- E.g. “/shoes?/” matches “/shoes/” or “/shoe/”
- Plus (+): Matches the previous character or group of characters at least once.
- E.g., “/shoes+/” matches “/shoess/” or “/shoesss/”
- This will be more useful once we discuss groups.
- Asterisks (*): Matches the previous character or group of characters 0 or more times.
- E.g. “/shoes*/” matches “/shoe/” or “/shoesss/”
- One of the most valuable patterns you will use is the “.*” combination, representing 0 or more of any character. For example, instead of constantly typing out “https://www.website.com{regex pattern}”, you can replace “https://www.website.com” with “.*”. This will be very useful in GA4 since the interface needs the data to match the regex to the pattern exactly.
- Same as the plus sign, this will be more useful once we discuss groups.
- Pipe (|): Used as an OR statement.
- E.g. “/shoes/123|/shoes/abc)” matches “/shoes/123” or “/shoes/abc”, but does not match “/shoes/a1b2c3”
Anchors
Anchors specify positions within text that a pattern must match, such as the beginning or end of the string.
- Caret (^): Matches when the subsequent characters are at the beginning of the string
- E.g. “^/shoes/” matches “/shoes/123-112233” but does not match “/yourwesbite.com/shoes/123-112233”
- Dollar sign ($): Matches when the string ends on the previous character(s)
- E.g. “/shoes/$” matches “/yourwebsite.com/shoes/” but does not match “/yourwebsite.com/shoes/123”
Escape
The “\” character is used when you want to interpret the subsequent character literally and not as a special regex character.
For example, “/shoes/search\?q=.*” matches “/shoes/search?q=sneakers”. The “?” is not used to match the previous character 0 or 1 times, but rather as the literal character.
Groups
Groups act as containers that allow you to denote parts of your pattern – a word, phrase or collection of characters – as a single unit.
- Round brackets or Parentheses ( ): Matches the exact order of the characters in the brackets.
- E.g. “(/shoes/)” matches “/shoes/123-112233” or “/womens/shoes/”
- Square brackets [ ]: Matches any order of the characters in the brackets.
- E.g. “/shoes/[123]” matches “/shoes/1” or “/shoes/2” or “/shoes/3”
- Hyphen (-): Creates a range of characters within the square brackets to match in any order.
- Common use cases:
- [A-Z] is all uppercase alphabetical characters
- [a-z] is all lowercase alphabetical characters
- [0-9] is all whole digits
- [A-Za-z0-9] is all alphanumeric characters
- Note that [A-z] is not the same as [A-Za-z]
- E.g. “/shoes/[0-9]+” matches “/shoes/102” or “/shoes/77”
- Common use cases:
- Curly brackets { }: Defines how many times the characters within square brackets need to appear in the string. There are three ways to do this:
- {n}: Exactly n occurrences.
- E.g. “/shoes/[0-9]{1}” matches “/shoes/1” or “/shoes/7”
- {n,}: At least n occurrences.
- E.g., “/shoes/[0-9]{1,}” matches “/shoes/6” or “/shoes/12” or “/shoes/123456789”
- {n,m}: At least n occurrences, but no more than m occurrences.
- E.g., “/shoes/[0-9]{1-3}” matches “/shoes/0” or “/shoes/14” or “/shoes/123”
- {n}: Exactly n occurrences.

Additional tips
I have several random (but useful) regex tips. I could not come up with where to put them, that’s why this section is just called “hot tips”.
- Regular expressions are case-sensitive. This means that “/Shoes/” is not the same as “/shoes/”. You may see a variation of “Matches regex (ignore case)” available, in which case this would mean that “/Shoes/” = “/shoes”. I would recommend using this option to avoid missing any data.
- Additionally, you can use “(?i)” in front of any strings where you want to avoid case sensitivity, e.g. “/(?i)shoes/” matches “/Shoes/” or “/shoes/”.
- Make use of brackets to simplify expressions. For example, if you want to match either “/shoes/” or “/boots/” rather than using “(^/shoes/$)|(^/boots/$)” you can combine these into “^/(shoes|boots)/$”.
- Test your regex using a site like regex101.com. One thing to note is that this site requires you to use the escape more than is necessary for GA4.
- GA4 regex is a “regex match”, not a “regex contains”. This means that your regex statement needs to match the dimension values exactly. For example, say you want to create a regular expression for every page that contains “/shoes/”. You will get no or limited data if you just set the regex to “/shoes/”. You should use the expression “.*/shoes/.*” since there could be more in the URL before or after the “/shoe/”.
- Regex is always generous, meaning it may sometimes include data you did not intend to use. Opposite of the .*, you will also want to ensure that you are using the hard stop $. This will ensure that you avoid unexpectedly including extra data.
- E.g., you want to capture the URL “www.site.com/shoes”. If you just set the regex to “.*/shoes”, then this will also include URLs like “…/shoes/123”. The correct pattern would be: “.*/shoes$”. However, in GA4, it’s the opposite, so there will be an assumed “$” at the end of the regex pattern.
So, let’s go back to that example from before. How would you create a regular expression to capture a URL in the format “/shoe/[3 digits]-[6 digits]”? Take a moment to try this out…
The answer: “.*/shoe/[0-9]{3}-[0-9]{6}$”
- .* says that anything can come before this in the URL
- /shoe/ is the part of the URL that tells us we are looking at a shoe page
- [0-9]{3}-[0-9]{6} is the format of the product ID for shoes
- $ says that the URL will end after the product ID

Regular Expressions in Google Analytics 4
In GA4, you may see up to four different selections that include regular expressions: “matches regex”, “partially matches regex”, “does not match regex”, or “does not partially match regex”. In most cases, “partially matches regex” will not be available, so if you don’t see this option, you should always assume it is the exact match (unless, of course, Google will change something in the future).
The difference between matches regex and matches partial regex is that partial matches regex acts like a “regex contains” rather than a “regex match”. Let’s use “www.website.com/shoes/123-112233” as an example.
Here, you could set the partial regex match as “/shoes/[0-9]{3}-[0-9]{6}$” but with the “matches regex” we would need to add the dot wildcard (.*) in front of the pattern, like “.*/shoes/[0-9]{3}-[0-9]{6}$” since it needs to be an exact match for the entire URL.
Standard report
There are two ways to filter a standard report: (1) use the “Add filter” button or (2) customize the report.
“Add filter” button
Go to the reports tab, find the report that you are interested in, and click the “Add filter” button. Choose the dimension you want to filter the report by and select one of “matches regex”, “partially regex”, “does not match regex” or “does not partially match regex” and enter the pattern.
In this case, we will filter the report only to include visitors from Canada or the USA. Select “Country” as the dimension, select “matches regex”, and enter the pattern “Canada|USA” (or whatever other countries you may be interested in).
Customize report
When you use the filter to customize a report, the filter element looks the same as above. However, once you are on the report you want to customize, click the pencil icon in the top-right corner and select “Add filter” from the Customize report tab on the new page.
Maybe you want to exclude any data from visitors in the USA or Canada, in which case you can reuse the same dimension and pattern from the previous example but use the “does not match regex” condition.
Learn more about all the ways you can customize a standard report here.
Exploration
When using the Exploration filter in GA4, you can filter your reports on dimensions and metrics. When you filter by a dimension, you have the option to filter using a regex pattern, which provides you with more freedom in how you view your data. The filter will use “matches regex” so the pattern will need to be an exact match.
In your exploration, you will find the Filters element at the bottom of the Settings tab. You can either click on the filter element and select a dimension or drag a dimension into the filter.
You will need to ensure that the dimension you want to filter on has been inputted into the report first.
Google Analytics 4 automatically tracks some events. If you have a report that looks at all events, it may be annoying having these auto-tracked events in there; you may just want to see the custom events you created.
Using the “does not match regex” condition, add in the events that you want to exclude for the report, separated by the pipe character (|), e.g. “page_view|scroll|user_engagement|session_start|click”.
As a reminder, a workaround to be able to mimic a “partially matches” condition is to employ the “.*” to look for partial matches. So, if you wanted all pages that contain “womens”, then you can use the pattern “.*womens.*” with the “matches regex” condition.
If you want a more comprehensive understanding of Explorations in GA4, check out this article.

Segments and Audiences
Segments allow you to slice and dice your data retroactively into subsets you can use in GA4 Explorations. Audiences will also enable you to divide your data into subsets, but it cannot be done retroactively. You cannot use audiences in Explorations, but you can use them in comparisons within the standard report. Learn more about the differences here.
Both of these subsets can be defined by multiple conditions, which can be filtered using regular expressions.
Segments
To create a segment, go to Explore and create a new report or use an existing report. In the Exploration interface, click the plus sign (+) next to Segments and select “User segment” (this is what we will use for the three examples below).
As with all the examples in this article, the segments you choose will depend on your business needs. These are just some examples to get you started and show you how to use regex.
- Excluding users in specific countries: “Country ID” does not match regex “US|CA”
- Users who have visited blog pages: “Page location” matches regex “.*/(blog|article|posts|news)/.*”
- Users who visited specific product pages: “Page location” matches regex “.*/product/[A-Za-z0-9-]+$”
Audiences
To create audiences, go to Admin > Data display > Audiences. Click “Create a custom audience”. You can use the same examples as above to provide inspiration for what you would want to capture in an audience. I would recommend reviewing the Google Analytics 4 Audiences article.
Custom channel groups
Custom channel groups allow you to categorize traffic sources into your defined channel groups. This means you can decide the names of the channels and the rules that dictate how to group the traffic sources into those channels.
One use case is that you may want to group all social media channels together. In this case, you can take all the platforms you would consider social media and separate them with the pipe symbol (|). You can put this into rounded brackets and then add (?i) in front so that the pattern ignores the case.
The final result will look like “(?i)(facebook|instagram|linkedin)”.
When creating conditions for custom channel groups, you will also be given the options of “partially matches regex” (which we will use in this example) and “does not partially match regex”. This means that you don’t need to worry about using “.*” to capture the entire string.
Internal traffic filtering
If you want to avoid tracking your own traffic on your site, you will want to use internal traffic filtering. This has some nuances, so I recommend reading how to exclude internal traffic in Google Analytics 4.
Go to Admin > Data collection and modifications > Data streams. Select the data stream you want to modify, scroll down to Configure tag settings > Show more > Define internal traffic.
Here, you can indicate the IP addresses you do not want to track. If you have multiple IP addresses, then you can use regex.
Define unwanted referrals
When you see “Referral” in your traffic data, it describes visitors who came to your site from another site without using a search engine. GA4 allows you to list specific domains that are not to be recognized as referral traffic but rather be labelled as direct traffic instead.
To reach this feature, go to Admin > Data collection and modifications > Data streams. Select the data stream you want to modify, scroll down to Configure tag settings > Show more > List unwanted referrals.
Now, you can define the referral domains you want to show as “direct” instead of “referral” in your GA4 reports.
If you want to exclude sub-domains, use the pattern “.*(paypal|example)\.com”.

Create and modify events
Directly in the GA4 interface you can create and modify events, which is helpful when you want to create a version of an existing event without needing to go through Google Tag Manager or a developer.
To reach this feature, go to Admin > Data display > Events > Create events.
Example 1: Create a new event by combining two other events
You may have two events that basically do the same thing, but in some cases, you still want to be able to view them as separate events. In this case, you can create a new event that combines the two events together.
Here, I am combining the form_submission and generate_lead events under a new event called form_completed.
Example 2: Create a new event based on 2 page URLs
Another scenario is that you may have multiple pages shown to a visitor when they have successfully completed some action, like a “thank you” page after a purchase or a “success” page after signing up for emails.
You can create an event that captures every time a user views one of these pages, called thank_you_page. The URL will vary based on your site, so tailor your regex pattern to match your site’s specific structure. However, for illustration purposes, we’ll use the following example: “https://.*(success|thank-you).*”.
Note that when you use the page_location parameter, the input must start with “http://” or “https://”.
Regex in Google Analytics 4: Final Thoughts
Hopefully, after reviewing this article, seeing the term “regex*” within Google Tag Manager and Google Analytics 4 is less confusing, and you can leverage this new tool to enhance your events and reports.
The most important thing is to understand the basic syntax and patterns. From there, you can create anything you can think of to fit your business needs, from tags to segments to report filtering.
If you have any other tips or tricks up your sleeve when it comes to regular expressions, leave it in the comments below!

0 COMMENTS