Regular Expressions for Google Analytics

Introduction

Creating Google Analytics filters requires knowledge of regular expressions.

A regular expression is a string of text that uses characters, numbers and wildcards to match patters in a string of characters.

Wildcards

Dot

  • Will match any single character, like a letter, number, punctuation or space. To match a literal period use \.

Backslash

  • Escape the special meaning of metacharacters.

Curly Braces

  • Use curly braces to repeat an exact number of times.

Parentheses

  • Put parentheses around a part of a pattern when you want to group it.

Square brackets

  • Put square brackets around a list of characters you want to match.

Dash

  • Creates a range in a list.

Vertical pipe

  • Use for alternation. (similar to OR)

Quantifiers and Repetition

Question mark

  • This will match zero or one of the previous items.

Plus

  • A plus sign added to a character or wildcard will match one or more of the previous items.

Asterisk

  • An asterisk added to a character or wildcard will match zero or more of the previous items.

General Examples

  • x* : Will matching nothing, x, xx, xxx and so on.
  • x+ : WIll match x, xx, xxx and so on.
  • x? : Will match x and xx but not xxx.
  • [0-9]: To match any digit.
  • [^0-9]: Does not match any digit.
  • [0-9]{4}: Will match any group of exactly four digits.
  • [0-9]{2,5}: Will match any two or five characters.
  • 3[12]: Will match numbers 31 and 32.
  • [^0-9]: Means anything that is not a digit.
  • help me$: will match only if that phrase appears at the end of a field.
  • \.(jpg|gif|bmp|png): Gets filenames with these extensions.
  • 31? matches 3, 31
  • 31+ matches 31, 311, 3111, …
  • 31* matches 3, 31, 311, 3111, …

Examples for Google Analytics

  • Match an IP Address: 192\.168\.0\.[1-5] will match the addresses 192.168.0.1 through 192.168.0.5
  • Match only one IP address: 192\.168\.0\.1
  • Match Blog directories in urls: .+?/blog/+?
  • Match any string: .*
  • Match specific URLs: .*index.\.php\?dl=video/trailers/(internet|theatrical)$ will match (anything)index.php?dl=video/trailers/internet and (anything)index.php?dl=video/trailers/theatrical