Appendix B. Regular Expressions

A regular expression (sometimes referred to as a regex) contains a mix of regular characters (like letters and numbers) and special characters that form a pattern. The pattern is applied to a piece of data and, if the pattern matches, the regular expression returns a positive result.

Many regular expressions include regular alphanumeric characters. For example, you may have a list of keywords, and you need to identify those keywords that contain “goo.” These three characters are a valid regular expression. Google Analytics will apply “goo” to the target data—in this case, the keywords. If “goo” matches any part of the data, the regex will return a positive result. Table B-1 shows some simple patterns and gives examples of data that match.

Table B-1. Regular expressions that contain only alphanumeric characters

Pattern

Description

Example matches

go

Match the characters go

google, go, merry-go-round, golf

bos

Match the characters bos

boss, boston, my boss, emboss

As illustrated in Table B-1, a regular expression does not need to be a complicated mix of special characters. However, it’s the special characters that make regular expressions powerful and flexible.

There are four basic types of special characters in regular expressions: wildcards, quantifiers, operators, and anchors.

Note

Google Analytics uses Perl Compatible Regular Expressions (PCREs). Any special characters that are specific to PCREs will work in Google Analytics.

Wildcards

Wildcards are used to indicate ...

Get Google Analytics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.