About regular expressions (regex)
Google Analytics supports regular expressions, also known as "regex", so you can create more flexible definitions for things like data filters, key events, segments, audiences, content groups, and custom channel groups.
In the context of Google Analytics, regular expressions are specific sequences of characters that broadly or narrowly match patterns in your Analytics data.
For example, if you wanted to create a view filter to exclude site data generated by your own employees, you could use a regular expression to exclude any data from the entire range of IP addresses that serve your employees. Let’s say those IP addresses range from 198.51.100.1 - 198.51.100.25. Rather than enter 25 different IP addresses, you could create a regular expression like 198\.51\.100\.\d* that matches the entire range of addresses.
Or if you wanted to create a view filter that included only campaign data from two different cities, you could create a regular expression like San Francisco|New York (San Francisco or New York).
Regex metacharacters
Wildcards
10, 1A
1.1 matches
111, 1A1
Examples
Anchors
10, 100, 10x
^10 does not match
110, 110x
Examples
110, 1010
10$ does not match
100, 10x
Examples
Groups
Also used to group other expressions (10) matches
10, 101, 1011
([0-9]|[a-z]) matches
any number or lower-case letter
Examples
012, 120, 210
Examples
Examples
Escape
216\.239\.32\.34 matches
216.239.32.34
Examples
Tips
Use simple expressions
Keeping your regular expressions simple makes it easier for another user to interpret and modify.
Match metacharacters
Use the backslash (\) to escape regex metacharacters when you need those characters to be interpreted literally. For example, if you use a dot as the decimal separator in an IP address, escape it with a backslash (\.) so that it isn’t interpreted as a wildcard.
Full regex vs partial regex
Google Analytics supports full regex by default. This means that, without adding metacharacters, the expression only returns exactly matching values. To create a partial regex and filter for dimensions containing a specific value, you can use metacharacters like ".*".
Example
- Default full regex: City matches regex "San Francisco" will return only "San Francisco".
- Partial regex: City matches regex ".*San Francisco.*" will return both "San Francisco" and "South San Francisco".