How to write regular expressions

Updated at:

This topic describes the matching rules for regular expressions.

Note

Only RE2 syntax is supported. This syntax is slightly different from PCRE. Note that regular expressions are case-sensitive by default. For more information, see the RE2 syntax.

Match only exact phrases

Usage example

Match the phrase

stock tips

Regex example

Example 1: (\W|^)stock\stips(\W|$)

Example 2: (\W|^)stock\s{0,3}tips(\W|$)

Example 3: (\W|^)stock\s{0,3}tip(s){0,1}(\W|$)

Notes

  • \W matches any character that is not a letter, number, or underscore. It prevents the regex from matching characters before or after the phrase.

  • In Example 2, \s matches a space character. {0,3} indicates that 0 to 3 spaces can appear between the words stock and tip.

  • ^ matches the beginning of a new line. This lets the regex match a phrase that appears at the beginning of a line with no preceding characters.

  • $ matches the end of a line. This lets the regex match a phrase that appears at the end of a line with no following characters.

  • In Example 3, (s) matches the letter s. {0,1} indicates that the letter can appear 0 or 1 time after the word tip. This regex matches both stock tip and stock tips. You can also use the ? character instead of {0,1}.

Match words or phrases in a list

Usage example

Match any word or phrase from the following list:

  • baloney

  • darn

  • drat

  • fooey

  • gosh darnit

  • heck

Regex example

(?i)(\W|^)(baloney|darn|drat|fooey|gosh\sdarnit|heck)(\W|$)

Notes

  • (...) groups all the words so that the \W character class can be applied to all words inside the parentheses.

  • (?i) makes the match case-insensitive.

  • \W matches any character that is not a letter, number, or underscore. It prevents the regex from matching characters before or after the words or phrases in the list.

  • ^ matches the beginning of a new line. This lets the regex match a word that appears at the beginning of a line with no preceding characters.

  • $ matches the end of a line. This lets the regex match a word that appears at the end of a line with no following characters.

  • | represents "or". This regex matches any one of the words in the list.

  • \s matches a space character. Use this character to separate words in a phrase.

Match words with different spellings or special characters

Usage example

Match the word "viagra" and some variations used by spam senders, such as:

  • vi@gra

  • v1agra

  • v1@gra

  • v!@gr@

Regex example

v[i!1][a@]gr[a@]

Notes

  • \W is not added, so other characters can appear before or after any variation of viagra. For example, this regex still matches viagra in the following text:

    viagra!! or ***viagra***

  • [i!1] matches the character i, !, or 1 in the second character position of the word.

Match all email addresses from a specific domain

Usage example

Match any email address from the domains yahoo.com, hotmail.com, and gmail.com.

Regex example

(\W|^)[\w.\-]{0,25}@(yahoo|hotmail|gmail)\.com(\W|$)

Notes

  • \W matches any character that is not a letter, number, or underscore. It prevents the regex from matching characters before or after the email address.

  • ^ matches the beginning of a new line. This lets the regex match an address that appears at the beginning of a line with no preceding characters.

  • $ matches the end of a line. This lets the regex match an address that appears at the end of a line with no following characters.

  • [\w.\-] matches any word character (a-z, A-Z, 0-9, or underscore), a period, or a hyphen. These are the most common valid characters in the first part of an email address. Note that \- (for the hyphen) must appear at the end of the character list within the square brackets.

  • The \ before the hyphen and period escapes these characters. This means the hyphen and period are treated as literal characters, not special regex characters. Note that the period does not need to be escaped inside square brackets.

  • {0,25} represents the number of characters from the preceding character set that can appear before the at sign, from 0 to 25. The Content Compliance email setting matches a maximum of 25 characters for each character set in a regular expression.

Match all IP addresses in a range

Usage example

Match all IP addresses in the range from 192.168.1.0 to 192.168.1.255

Regex example

Example 1: 192\.168\.1\.

Example 2: 192\.168\.1\.\d{1,3}

Notes

  • The \ before each period escapes the period. This means the period is treated as a literal character, not a special regex character.

  • In Example 1, there are no characters after the last period. The regex matches all IP addresses that start with 192.168.1., regardless of the numbers that follow.

  • In Example 2, \d matches any digit from 0 to 9 after the last period. {1,3} indicates that 1 to 3 digits can appear after the last period. In this case, the regex matches all complete IP addresses that start with 192.168.1.. Note that this regex also matches invalid IP addresses, such as 192.168.1.999.

  • Domains are grouped in the (...) format, where the `|` character separates domains and represents 'or'.

Match alphanumeric formats

Usage example

Match a company's purchase order numbers. These numbers can have various formats, such as:

  • PO nn-nnnnn

  • PO-nn-nnnn

  • PO# nn nnnn

  • PO#nn-nnnn

  • PO nnnnnn

Regex example

(\W|^)po[#\-]{0,1}\s{0,1}\d{2}[\s-]{0,1}\d{4}(\W|$)

Notes

  • \W matches any character that is not a letter, number, or underscore. It prevents the regex from matching characters before or after the number.

  • ^ matches the beginning of a new line. This lets the regex match a number that appears at the beginning of a line with no preceding characters.

  • $ matches the end of a line. This lets the regex match a number that appears at the end of a line with no following characters.

  • [#\-] matches a hash sign or a hyphen after the letters po. {0,1} indicates that one of these characters can appear 0 or 1 time. Note that \- (for the hyphen) must appear at the end of the character list within the square brackets.

  • \s matches a space. {0,1} indicates that a space can appear 0 or 1 time.

  • \d matches any digit from 0-9. {2} indicates that exactly 2 digits must appear in this position of the number.