How to use Regular Expressions (Regex) in Google Analytics
Loves Data
What are regular expressions in Google Analytics?
Regular expressions (regex for short) can help speed up your use of Google Analytics as well as add extra flexibility to the way you include (or exclude) particular data and information from reports. You can think of regular expressions as a way to search for and match particular information. If you’re just getting started, then regular expressions can be daunting, but remember that you don’t need to memorize them and there are plenty of resources to help you!
Get the Regular Expressions Cheat Sheet
Download the cheat sheet. It’s a handy resource for the regular expressions you can use in Google Analytics. Get the Regular Expressions Cheat Sheet for Google Analytics PDF.
Download the Regular Expressions Cheat Sheet
Let’s start with a simple scenario. Let’s say you want to filter a report to only show you two specific folders, the /products/ folder and the /services/ folder. To do this, you can use the following regular expression:
^/(products|services)/
You will notice we’re using some special characters to only include the two folders in our report. These are the ^ (caret) and the | (pipe) and we’re using them to create our regular expression. You can enter your regular expression in the table filter (search box) inside your Google Analytics reports. Here we can see the regular expression in the pages report:

Now let’s look at how this regular expression works.
We start with the caret on the left. This is like saying ‘match to the beginning’ – it means that nothing can come before the caret.
For example, if we used ^/products/ as our regular expression, then we would only match pages that start with /products/ like /products/cameras/ and /products/monitors/, while pages like /support/products/ and /about/products/ would not be matched.
The brackets create a list. This lets us match items inside the brackets.
The pipe, which you should be able to find above the backslash on your keyword (if you are using an English keyboard), looks like a vertical line. This creates an ‘OR’ statement.
When we combine the bracket with the pipe, we can match the different items contained in our list. For our example, we’re matching products or services. You can also extend this if you need to match more items. For example, (products|services|about|support) would match products, services, about or support.
We can see that outside the brackets, we have a forward slash on both sides. This means that there must be a forward slash before and after products or services. So we are matching /products/ or /services/ and either of these must be at the beginning of a page for it to be included in our report.
To go a step further, you might have pages (or even subfolders) within the page structure, for example, /products/featured and /products/cameras/. If we used the same regular expression, then the page and subfolder would be matched in our report. If you didn’t want this page and subfolder included and you just wanted the products and services folders, you could add a dollar sign to the end of the expression. For example, ^/(products|services)/$ would only ever match /products/ or /services/, it would not match with anything before or after.
Where can you use regular expressions in Google Analytics?
Regular expressions can be used in Google Analytics to speed up report customization and for extra flexibility when you’re configuring your reporting views. Here’s a quick summary of all of the places you can use regular expressions in Google Analytics:
- Table filters
- Custom segments
- Filtering dashboard widgets
- Filtering custom reports
- Goals
- Filters
- Audiences
- Content grouping
- Channel grouping
Table filters
When you search within a standard (or custom report), you are using a table filter. You will find the option to filter a report below the graph on the right.

This search (or table filter) accepts regular expressions, so you can control what is included (or excluded) from the report. You can also use regular expressions when you use the ‘Advanced’ option for the table filter – just make sure you select ‘Matching RegExp’ for the match type.

Custom segments
You can use regular expressions when creating custom segments. Here’s an example of a custom segment that doesn’t use regular expressions:

Using regular expressions makes it quicker to create the segment:

Filtering dashboard widgets
You can add filters to the widgets you add to your custom dashboards inside Google Analytics. Here we can see we’re using a regular expression to only include certain types of products in our widget:

Filtering custom reports
You can use filters to only include certain information when you create a custom report. For example, you can use a regular expression to only include certain sections of your website’s content when creating a custom report.

Goals
Regular expressions can be used when you configure goals based on pages and events. For example, you can use a regular expression to match multiple thank you pages when configuring a destination goal.

Filters
You can use regular expressions in the filters you apply to your reporting views. Here we can see an advanced filter that uses regular expressions to capture the request URI (page path) and hostname (domain name) from the website:

The request URI and hostname are then combined to modify the way pages are presented in the Behavior reports.
Audiences
You can create audiences in Google Analytics for targeting ads in your Google Ads account and for reporting. Creating an audience list is very similar to creating a custom segment (in fact, you can even use a segment to build your audience).

Content grouping
You can create custom content classifications in Google Analytics using content groups. There are different ways to create groups, but when you use ‘Rule Definitions’, you have the option of using regular expressions to match your content.

Channel grouping
Regular expressions can be used when configuring channel groupings and custom channel groupings in Google Analytics. Channel grouping is similar to content grouping, but you can use them to classify the different ways people are finding your website.

Tip: Selecting ‘Matches Regex’ for your channel grouping doesn’t work in the same way as the other areas we’ve covered in Google Analytics – it matches exactly. For example, if you select regular expression matching and enter facebook|twitter, then the grouping will only match facebook or twitter it won’t match facebook.com (which would be matched in other areas of Google Analytics). This means if you want flexible matching, you will need to adjust your regular expression. Continuing our example we could use (facebook|twitter).* to match facebook, facebook.com, twitter and twitter.com.
Important characters for regular expressions in Google Analytics
Caret ^
The caret lets you match to the beginning. It’s like saying ‘starts with’.
For example, ^demo would match demos and demonstration but not my demo. There can’t be anything before demo, if there is, then it won’t be matched.
Dollar sign $
The dollar sign says that there can’t be anything after your expression. It’s like saying ‘ends with’.
For example, demo$ would match my demo but not demonstration. There can’t be anything after demo.
Question mark ?
The question mark lets you match zero or one of the previous item. It’s like saying ‘one or none’.
For example, demos?123 would match demo123 and demos123 but not demoA123. It is saying there needs to be an ‘s’ or no ‘s’ to match.
Asterisk *
The asterisk is used to match zero or more of the previous item.
For example, goo*gle would match gogle, google and goooogle but not goggle. For the ‘o’ it is saying there needs to be zero or more of them to match.
Full stop .
The full stop matches any single character.
For example, go.gle would match google and goggle but not gogle.
When you combine a full stop with an asterisk, you can match everything.
For example, my.* would match ‘my’ followed by anything, including my demo, mydemo and my google. However, it would not match demo or google.
Pipe |
The pipe lets you create a list. It’s like saying ‘or’.
For example, demo|example would match demo and example, but not analyze.
Brackets ( )
Brackets create a group. You can use them with a pipe to create a list.
For example, (demo|example)-page would match demo-page and example-page, but not other-page.
Tip: Brackets are also used to store a value when you use them in an advanced filter.
Backslash \
The backslash lets you escape any special characters you want to use literally. This is useful if you need to match a character that can also be used for your regular expression, like a question mark (?) or a dollar sign ($).
For example, USD\$10 would match USD$10, USD$100 but not USD10 or 10.
Get the Regular Expressions Cheat Sheet
Download the cheat sheet. It’s a handy resource for the regular expressions you can use in Google Analytics. Get the Regular Expressions Cheat Sheet for Google Analytics PDF.
Download the Regular Expressions Cheat Sheet
Five tips for regular expressions
Watch my video, where I cover five tips for using regular expressions in Google Analytics:
Comments