In this post, I will explain how to use regular expressions (regex) in Google Looker Studio, Google Analytics, and also Tag Manager. I will also share some advanced examples and use-cases.
In this post, I will explain how to use regular expressions (regex) in Google Looker Studio, Google Analytics, and also Tag Manager. I will also share some advanced examples and use-cases. And last but not least, I will update this post whenever stumbling upon new methods to put regex to use.
This article is written with a focus on Google Looker Studio. However, the regex functions can also be applied in Google Analytics as well as Tag Manager.
Let’s commence.
The definition of regular expression according to Wikipedia is:
A regular expression, regex or regexp (sometimes called a rational expression) is a sequence of characters that define a search pattern.
There are many different types of regex used by many different programming languages and programs. Google supports a simple yet powerful variant of regex for Data Studio and Analytics.
In the table below you find the symbols used in regular expressions. Click on the name of the symbol to jump to the detailed information.
The dot matches any single character. Basically it’s a wildcard. For example:
The number of dots define the number of characters. By combining the dot symbol with the asterisk symbol you can match any character and any number of characters.
The question mark sets the preceding character as optional. This can be useful for catching misspellings or to manipulate numbers:
A common use-case for the question mark symbol is in handling IP-addresses. Check it out here.
The plus means the previous character can repeat itself 1 or more times. It’s very similar to the asterisk which means 0 or more times.
An example of a plus in action:
The asterisk matches the preceding character 0 or more times. Like the plus symbol but in stead of a minimum of 1 character, the asterisk also matches if the character before is not a part of the result.
So:
Awesome combo: Dot & Asterisk to mach any character, any number of times.
The pipe symbol means OR. If you need to match more variations in one single query, the pipe will come in handy!
An example: ‘/gift/|/giftcard/’
Matches any URL which contains either /gift/ or /giftcard/.
The caret symbol is used to define the beginning of any query. So if you start with a caret, the character(s) that come after only match if it’s the beginning of the result.
For example: ‘^/giftcard/’ only matches pages that begin with /giftcard/. If your website also contains URL’s like /buy/giftcard/, these will not match the query.
Like the caret but the other way around 🙂 The dollar sign defines the ending of any query. In other words, nothing comes after the dollar sign. Like when Gandalf puts his staff down, you ain’t getting past.
Example: ‘giftcard$’ matches ‘giftcard’, ‘buy-giftcard’ or ‘shop giftcard’. It does not match ‘giftcards‘ or ‘/giftcard/‘.
Matches enclosed characters in exact order in a string. Parentheses are also used to group expressions.
‘AB|C’ = ‘AB’ or ‘C’
‘A(B|C)’ = ‘AB’ or ‘AC’
Parentheses are very handy in handling IP-addresses. Read all about it.
With square brackets you can create lists. Put multiple character inside the square brackets and the expression will match 1 of those.
So for example ‘product[ABC]’ matches:
It does not match ‘productAB’ or ‘productAC’.
Curly brackets (or braces) repeat the preceding character a specific number of times. For some reason, these brackets are not mentioned in any Google Analytics documentation. Curly brackets can contain 2 numbers, separated with a comma like so {2,5}. The first number states the minimum times a character is repeated and the second number defines the maximum. The maximum setting is optional.
The dash symbol is used in combination with the square brackets. Put them together and you can create powerful lists. For example ‘[A-Z]’ means every capital letter in the alphabet. It works the same for digits: ‘[0-9]’.
The backslash functions as an escape in a regex. Need to use a symbol from the list above but not as a regex function? Escape it using de backslash. For example, you want to search for a string containing dots like an IP-address 123.456.10.10 you can escape the function of the dot with the backslash like so: 123\.456\.10\.10
Below you will find a few Shorthand Character Classes (Regex Shortcuts) that are supported in Google Analytics and Google Data Studio. Shorthands are a simple, more human-readable shortcuts for specific regex characters.
Check out the combo’s to see examples.
The ‘\w‘ matches any ASCII character so letters, digits, and the underscore. It is the shortcut for ‘[A-Za-z0-9_]‘.
The ‘\d‘ matches any number (digit). It’s short for ‘[0-9]’.
The ‘\s’ matches a space. It can be used to separate or count words.
Check out this combo to learn how to use these shortcuts.
And now, brace yourself for the real power of Regex: combo’s!
This is the Regex combo I use the most in Data Studio or Analytics. The dot means any character and the asterisk says that the dot can happen 0 or more times. So basically this means any character and any number of characters. For example:
‘/sign-up/.*/thank-you’ matches both
/sign-up/newsletter/thank-you
as well as
/sign-up/mailing/thank-you
or any other text or numbers in between the forward slashes.
Say you want to filter on any number of words used as a search query on your website. Using the regex shortcuts in combination with the carrot and dollar sign you can do just that!
Match 1 word:
I’ll skip two words because I’m a rebel! Matching 3 words:
Now you can create badass segments filtering short- and longtail search queries and be able to see the impact on your website goals.
There are a few tricks to matching IP-addresses with regex. First, you will need to escape the dots which are used in every IP-address.
If you need to match a range of different IP-addresses you can use the list and the curly brackets quantifier together for a powerful combo. For example, you want to match all IP-addresses from 123.456.789.0 up to 123.456.789.99
That was easy right? But what if you only need to match 0-25 and 55-70?
Finally, if you’re looking to match any number in the last octet of the IP-address you can also use the shortcut ‘\d’
Using regex to filter IP-addresses? Check if you are using IP anonymization. If you are, always add ‘0’ at the end of every IP-address. Or use ‘\d’ in the last octet.
Correctly matching e-mail addresses can be done with the regex below.