Get startedGet started for free

What are regular expressions?

1. What are regular expressions?

Regular expressions are an important tool that the Google Marketing Platform's suite of products uses frequently, including Google Analytics and Google Tag Manager.

2. Digital marketing table

Before we dive into regular expressions, recall the digital marketing table we have been using.

3. Digital marketing table

What if we wanted to see how user campaigns perform and highlight them in the table? Of course, we could highlight R users and Python users manually, but what if the table was sorted differently or if more rows were added?

4. Answer: use regular expressions

Regular expressions are a special search pattern, made of a sequence of special characters. These are also known as regex. Regular expressions are used to search within strings and can filter within categories, like campaign names. What would the regular expression look like to only match R users and Python Users, and not include the DataCamp brand campaigns?

5. Regular expression for users

To match all campaigns ending in the word users, we will need to write a regular expression that includes the following items. The first is a period-asterisk combination to match any number of characters preceding the word "users". Next, we need to account for the letter u being lower or upper case, separating each variation with a pipe character. To make one of the versions of the letter u required, we wrap this in brackets. Lastly, we add the rest of the word after the brackets. Now that we have written our first regular expression, let's dive deeper and explore some more regular expression characters. We will explore three of the regular expression metacharacter categories; wildcards, anchors, and groups.

6. Basic regular expression characters: wildcards

We begin with characters in the wildcard category. Wildcard characters match string characters with no restrictions on what kind of characters. The period matches any character. D period g will match the letter o in dog or i in dig. The asterisk matches 0 or more times. The period-asterisk combination matches all the letters in the word dog. The question mark matches 0 or 1 time, which works great when matching words that may be plural sometimes. The dog question mark combination matches dog or dogs. The plus sign matches one or more times. The period-plus combination must have at least 1 character and would match dogs but not dog, since it requires one or more characters after the word dog. The pipe symbol between x and y would match either x or y. Dog and cat separated by a pipe will match either dog or cat. Lastly, placing the backslash immediately before the character will escape or cancel the regex meaning of a character. To match a question mark, add a backslash before the special character to nullify the regular expression meaning.

7. Basic regular expression characters: anchors

The anchor characters designate the first or last character in a string to match. The caret indicates the first character in a string, while the dollar sign indicates the last character. For example, let's match the string "The dog likes to dig" using anchors. The caret-capital T combination matches the beginning of the string, and the lowercase g-dollar sign combination matches the end.

8. Basic regular expression characters: groups

Group metacharacters specify a group or range of characters to match. The brackets indicate a list of items that the string must contain for it to be matched. In this case, the word dog must begin with a lower or upper case d. The curly braces matches x number of times. The example matches the first two characters in a string, meaning the letters d and o in the word dog.

9. Let's get to work!

This was a lot of information. Let's get to work and practice using regular expressions!