How to use Regular Expressions (Regex) in Google Analytics

Regex in Google Analytics

What are regular expressions in Google Analytics?

Regular expressions (or 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 Regex Cheat Sheet for Google Analytics PDF.

Download the Regex 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 /shop/apparel folder and the /shop/lifestyle folder. To do this, you can use the following regular expression:

^/shop/(apparel|lifestyle)$

You will notice we’re using some special characters to only include the two folders in our report. These are the ^ (caret), the | (pipe), and the $ (dollar sign). We’re using them to create our regular expression. When you create a custom Exploration report, you can apply a filter that includes a regular expression. Here we can see an Exploration report with the filter applied:

Exploration report in Google Analytics with a regex filter applied

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 ^/shop/apparel as our regular expression, then we would only match pages that start with /shop/apparel, while pages like /shop/lifestyle would not be matched.

Using ^/shop/ means we want /shop/ to be at the start. This will exclude any other folders. For example, if we have /checkout, /search, /shop/apparel, /shop/lifestyle, and /shop/clearance, this regular expression immediately excludes the /checkout and /search folders.

Next, 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 apparel or lifestyle. You can also extend this if you need to match more items. For example, (apparel|lifestyle|stationery|clearance) would match apparel, lifestyle, stationery or clearance.

Finally, we have the $ which means there can't be anything following our regular expression. This means we're only matching /shop/apparel and /shop/lifestyle. If there's anything after these, like /shop/apparel/, /shop/apparel/womens or anytnig else, these won't be included in our report.

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 property and data streams. Here’s a quick summary of all of the places you can use regular expressions in Google Analytics:

  • Standard report filters
  • Exploration reports
  • Segments
  • Audiences
  • Creating events
  • Modifying events
  • Channel groups
  • Data streams

Standard Report Filters

If you have editor or administrator permission for the Google Analytics property, you can edit the standard reports. If you see the pencil icon on the top right corner of a standard reports, you can edit it and apply a regular expression filter to the report.

Here we can see a filter that uses a regular expression to only include two event names in the report:

Filter being applied to a standard report with regex matching

Exploration Reports

When you create an Exploration report you can apply filters to the report that use regular expressions. Before creating the filter you should start by adding the dimension or dimensions you want to use.

Regex filter applied to exploration report

Segments

You can use regular expressions when creating custom segments in Google Analytics. Here’s an example of a custom segment that uses a regular expression:

Segment using regex in a Google Analytics exploration report

Audiences

You can create audiences in Google Analytics for reporting and targeting in Google Ads. Creating an audience list is very similar to creating a custom segment (you can even use a segment to build your audience).

Google Analytics audience using regex matching

Creating Events

If you're creating a new event in the Google Analytics interface using the 'Create Event' option, you can use regular expressions as your matching condition. This allows you to match multiple parameter values in your condition.

Creating a new event in Google Analytics with regex matching

Modifying Events

Similar to creating new events in the interface, you can use regular expressions when modifying events.

Modifying an event in Google Analytics with regex

Channel Groups

Regular expressions can be used when configuring channel group in Google Analytics. Channel groups lets you combine marketing channels so they're aggregated in your reports. They allow you to classify the different ways people are finding your website.

Regex in channel group configuration

Data Streams

You can use regular expressions in a number of places when configuring your data streams. Settings that accept regular expressions include:

  • Domain configuration for cross-domain tracking.
  • Defining internal traffic based on IP addresses.
  • Listing unwanted referrals which will be excluded from reports.
Matching domains using regex in the Google Analytics data stream

Regular Expression Characters 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.

Matching Conditions

When you’re using regular expressions in Google Analytics, you’ll often see different match types available – especially when you're setting up filters in reports, creating audiences, or configuring events. Here’s what each one means and when to use them.

Matches regex

This means the entire value must match the regular expression exactly.

Example: If you use ^/products/, it will match /products/page1 – but not /shop/products/page1, because the value doesn't start with /products/.

Use this when you want precise control over what is matched.

Partially matches regex

This is a more flexible option – the regex only needs to match part of the value.

Example: Using products will match /products/page1 and /shop/products/page1.

Use this when you want to find values that contain a specific word or pattern, no matter where it appears.

Does not match regex

This means the entire value must not match the regular expression.

Example: If your regex is ^/products/, then /about/ or /services/ will be included – but /products/ or /products/cameras will be excluded.

This is handy when you want to filter out specific values that exactly match a pattern.

Does not partially match regex

This means no part of the value can match the regular expression.

Example: Using products will exclude any value that contains the word products, like /products/page1, /shop/products, or even /products.

This is useful when you want to remove all results that mention or resemble a certain term.

Are there any places you cannot use regular expressions in Google Analytics?

Yes, there are a number of features that don't currently support regular expressions. These include:

  • Comparisons
  • Search bar in standard reports
  • Filters in the advertising reports

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 Regex Cheat Sheet for Google Analytics PDF.

Download the Regex Cheat Sheet


Tips for regular expressions

Watch my video, where I cover tips for using regular expressions in Google Analytics:

Comments