Regular Expressions: What are They Good for?

Regular expressions what are they good for

Regular expressions what are they good for

Regular expressions what are they good for

If you’re involved in digital analytics, chances are you’ve come across the term ‘Regular Expression’. But do you actually know what they are and how much they’re used in the world of digital analytics?

Randall Munroe, xkcd.com/208

What is a regular expression?

At its most basic, a regular expression – or ‘regex’ for short – is just a way of defining a pattern in some text. For example, if you have a text document and you want to find every mention of either Google Analytics or Google AdWords, you’re really searching for the pattern that starts with Google followed by a space and then either Analytics or AdWords. One way of doing that would be by letting your document loose on a tool that lets you use regex patterns to search and telling it to look for the pattern /Google (Analytics|AdWords)/g.

Ok, that is a pretty simple example, but the beauty of regular expressions is that they can be amazingly versatile … and, if you like, very complicated! They also have a host of uses in other tools, as well as Google Analytics.

We’ve blogged before on regular expressions basics and how to build them, so we won’t repeat ourselves here. If you’re looking for a recap, find out more and download our free ‘cheat sheet’ to help you while you are at it.

Where are regular expressions used?

You may have already come across regular expressions in Google Analytics, and perhaps avoided using them. However, you could be missing out on a great way to get more valuable data out of Google Analytics.

Here’s just a few examples of where you can use them to great effect:

Filtering table rows Almost all standard reports in Google Analytics give you an option to filter table rows. But did you know that if you click the ‘advanced’ button, you can use a regex to take more control over the filter? For example, let’s say that you want to filter your ‘landing page’ report to see details only for landing pages that were blog posts published in 2012 to 2014. Depending on how your URLs are structured, this could be quite hard to do as a basic text filter, but a regex could handle it with ease.This example shows the regex RegEx used:

This example shows the regex used: ^\/blog\/201[2-4]\/

This example shows the regex used: ^\/blog\/201[2-4]\/

This example shows the regex used: ^\/blog\/201[2-4]\/

Creating segments Custom defined segments are great for isolating particular groups of users, or their website sessions, for analysis. And, generally speaking, wherever you can enter text for a dimension or metric, you can use a regex to hone in on exactly what you want.

For example, let's say you want to create a custom segment showing data for cases where the ecommerce product name identifies a red, green or blue shirt or t-shirt. In this case, you could use the regex ^(red|green|blue)-t?shirt$ in your segment definition:

For example, let's say you want to create a custom segment showing data for case

Your custom segment will then match where the product name is any of the following: red-shirt, red-tshirt, green-shirt, green-tshirt, blue-shirt, or blue-tshirt, but nothing else.

Creating goals and view filters Google Analytics lets you use a regex to define page URLs and other text strings to match your goal definitions and view filters. So, if you need to create a filter that excludes traffic from IP addresses in a particular range, or if you want a goal where the destination page is not always exactly the same static URL, regex patterns can come to your rescue.

Creating custom dashboards and reports There is barely a place in Google Analytics where you can’t use regex. Would you like to add a widget to your custom dashboard showing traffic sessions for campaigns that start with spring2015, optionally followed by a dash and 2–4 digits, followed by a dash and an a, b or c? Easy! Just use the regex ^spring2015(-\d{2,4})?-[a-c]$

Creating custom dashboards and reportsThere is barely a place in Google Analytic

What are you waiting for? Dive in with regex in your Google Analytics

The ability to use regular expressions to fine tune your reporting and analysis is available from almost everywhere in Google Analytics. But it can only help if you’re making use of it. So give it a go and dive into your accounts today and change how you look at your reports.

If you struggle with constructing regular expressions and remembering all the rules (and don’t worry, because everyone does), check out this cheat sheet that we’ve put together just for these moments!

How do you use regular expressions in Google Analytics?

We’d love to hear your inventive ways of digging into your Google Analytics data with the help of a nifty regex. Feel free to share in the comments below.

This article was updated on 2 February 2025

Comments