Docsity

Regular Expressions (RegEx) for Analytics: Creating Filters and Patterns, Exercises of Mathematics

Toggle sidebar
bg1
Regular expressions (RegEx)
Page 1 of 8
Drawn from: support.google.com/analytics
About regular expressions (regex)
Analytics supports regular expressions so you can create more flexible definitions for things like view
filters, goals, segments, audiences, content groups, and channel groupings.
In the context of 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
.
Matches any single character (letter, number or symbol)
1. matches
10, 1A
1.1 matches
111, 1A1
Examples
?
Matches the preceding character 0 or 1 times
10? matches
1, 10
Examples
+
Matches the preceding character 1 or more times
10+ matches
10, 100
Examples
*
Matches the preceding character 0 or more times
1* matches
1, 10
Examples
|
Creates an OR match
Do not use at the end of an expression
1|10 matches
1, 10
Examples
pf3
pf4
pf5
pf8

Partial preview of the text

Download Regular Expressions (RegEx) for Analytics: Creating Filters and Patterns and more Exercises Mathematics in PDF only on Docsity!

Drawn from: support.google.com/analytics

About regular expressions (regex)

Analytics supports regular expressions so you can create more flexible definitions for things like view filters, goals, segments, audiences, content groups, and channel groupings.

In the context of 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

. Matches any single character (letter, number or symbol) 1. matches 10, 1A

1.1 matches 111, 1A

Examples

? Matches the preceding character 0 or 1 times 10? matches 1, 10

Examples

  • Matches the preceding character 1 or more times 10+ matches 10, 100

Examples

  • Matches the preceding character 0 or more times 1* matches 1, 10

Examples

| Creates an OR match

Do not use at the end of an expression

1|10 matches 1, 10

Examples

Anchors

^ Matches the adjacent characters at the beginning of a string

^10 matches 10, 100, 10x

^10 does not match 110, 110x

Examples

$ Matches the adjacent characters at the end of a string

10$ matches 110, 1010

10$ does not match 100, 10x

Examples

Groups

( ) Matches the enclosed characters in exact order anywhere in a string

Also used to group other expressions

(10) matches 10, 101, 1011

([0-9]|[a-z]) matches any number or lower- case letter

Examples

[ ] Matches the enclosed characters in any order anywhere in a string

[10] matches 012, 123, 202, 120, 210

Examples

  • Creates a range of characters within brackets to match anywhere in a string

[0-9] matches any number 0 through 9

Examples

Escape

\ Indicates that the adjacent character should be interpreted literally rather than as a regex metacharacter

. indicates that the adjacent dot should be interpreted as a period or decimal rather than as a wildcard.

216 .239.32.34 matches

City matches regex London|Paris

You can also use the pipe within parentheses. For example, here’s another way to create the expression to match your beach-sandal SKUs:

(MN|WM)BS\d+

(MN OR WM) BS (character code for beach sandals) \d (numeric character)

  • (one or more times)

Square brackets [ ] and the hyphen -

Square brackets [ ]

Use square brackets to create a set of characters to match.

When you include a series of characters in brackets, your expression matches 1 of those characters.

For example, the expression PART[123] matches:

PART PART PART

The expression doesn’t match:

PART PART PART

Hyphen -

Use the hyphen along with the brackets to create a range of characters to match.

For example, if you need to match any uppercase letter, you can specify [A-Z].

If you need to match any digit, you can specify [0-9].

If you needed to match all of the part numbers above, you could use the following expression:

[A-Z]+[0-9]+

[A-Z] (matches any uppercase letter)

  • (one or more times) [0-9] (matches any digit)
  • (one or more times)

Question mark (?), plus sign (+), asterisk (*)

Question mark (?)

The question mark (?) matches the preceding character 0 or 1 times.

For example, 10? matches:

1 10

Example

Match an IP address with 1 or 2 digits in the last section.

For example, 216.239.32.\d\d? matches:

216.239.32. 216.239.32.

This example uses the backslash to escape the decimal, and uses \d to match any digit.

Plus sign (+)

The plus sign (+) matches the preceding character 1 or more times.

For example, 10+ matches:

10 100 1000 etc.

Example

Match an IP address with 1 or more digits in the last section.

For example, 216.239.32.\d+ matches:

216.239.32. 216.239.32. 216.239.32.

This example uses the backslash to escape the decimal, and uses \d to match any digit.

Asterisk (*)

The asterisk or star matches the preceding character 0 or more times.

For example, 10* matches:

1 10 100 1000

sees a backslash, it knows that it should interpret the next character literally. A regular expression to match the IP address 0.0.0.0 would be:

0.0.0.

Use the backslash to escape any special character and interpret it literally; for example:

\ (escapes the backslash) [ (escapes the bracket) { (escapes the curly brace) . (escapes the dot)

Caret (^)

Use the caret to match the following adjacent characters at the beginning of a string.

For example, ^St matches:

St art here St and here St op here

However, ^St doesn’t match:

1 Start here 2 Stand here 3 Stop here

The preceding 3 lines start with a digit and a space, not with the letters “St”.

Use this type of regular expression to create segments, filters, or goal steps that match a URI. For example, if you need to isolate data for a particular directory of pages, you can use expressions like:

^/mens/ (matches www.example.com /mens/ ) ^/womens/ (matches www.example.com /womens/ )

Dollar sign ($)

Use the dollar sign to match the preceding, adjacent characters at the end of a string.

For example, end$ matches:

Temporarily susp end We’re going off the deep end Match the characters that prep end

However, end$ doesn’t match:

Temporarily suspend. We’re going off the deep end. Match the characters that prepend.

The preceding 3 lines all end with nd. rather than the end characters you’re matching with the regular expression.

Use this type of regular expression to create segments, filters, or goal steps that match a URI. For example, if you’re you’re testing the efficacy of a new .htm version page vs. an older .html version, you can identify the versions separately with regular expressions like:

email-signup.htm$ email-signup.html$

Use the slash to escape the dot and ensure that it is interpreted literally.

Test your regular expressions

An easy way to test a regular expression on your own data is to navigate directly to a report in your view and use the advanced table-filter feature.

For example, to try out a regular expression that filters pages:

  1. Sign in to Google Analytics.
  2. Navigate to your view.
  3. Open Reports.
  4. Select Behavior > Site Content > All Pages.
  5. Above the table, click advanced.
  6. Create a condition: [select: Include or Exclude] Page [select: Matching RegExp] [enter regular expression]
  7. Click Apply.