Notification

Duet AI is now Gemini for Google Workspace. Learn more

Examples of regular expressions

The following examples illustrate the use and construction of simple regular expressions. Each example includes the type of text to match, one or more regular expressions that match that text, and notes that explain the use of the special characters and formatting.

For additional instructions and guidelines, see also Guidelines for using regular expressions and RE2 Syntax. See also Set up rules for content compliance.

Important: We support RE2 Syntax only, which differs slightly from PCRE. Regular expressions are case-sensitive by default.

Note: Examples shown below can be useful as starting points for more complex regular expressions. However, for matching a single word, we suggest that you use the Content compliance or Objectionable content settings.

Match exact phrase only
Usage example Match the phrase stock tips.
Regex examples 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’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the phrase.
  • In example 2, \s matches a space character, and {0,3} indicates that from 0 to 3 spaces can occur between the words stock and tip.
  • ^ matches the start of a new line. Allows the regex to match the phrase if it appears at the beginning of a line, with no characters before it.
  • $ matches the end of a line. Allows the regex to match the phrase if it appears at the end of a line, with no characters after it.
  • In example 3, (s) matches the letter s, and {0,1} indicates that the letter can occur 0 or 1 times after the word tip. Therefore, the regex matches stock tip and stock tips. Alternatively, you can use the character ? instead of {0,1}

Match word or phrase in a list
Usage example Match any word or phrase in 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, such that the \W character class applies to all of the words within the parenthesis.

  • (?i) makes the content matching case insensitive.

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

  • ^ matches the start of a new line. Allows the regex to match the word if it appears at the beginning of a line, with no characters before it.

  • $ matches the end of a line. Allows the regex to match the word if it appears at the end of a line, with no characters after it

  • | indicates an “or,” so the 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 word with different spellings or special characters

Usage example

 

Match the words fast cash and some of the obfuscations that spammers use, such as:
  • f@st c@sh
  • f@$t c@$h
  • fa$t ca$h
Regex example

f[a4@][s5\$][t7] +c[a4@][s5\$]h

Notes
  • \W isn't included, so that other characters can appear before or after any of the variants of fast cash. For example, the regex still matches fast cash in the following text:

Fast cash!! or ***f@st ca$h***

  • [a4@] matches the characters a, 4, or @ in the second character position of the word, reflecting common letter substitutions spammers use to evade simple text matches.

Match any email address 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’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the email address.
  • ^ matches the start of a new line. Allows the regex to match the address if it appears at the beginning of a line, with no characters before it.
  • $ matches the end of a line. Allows the regex to match the address if it appears at the end of a line, with no characters after it.
  • [\w.\-] matches any word character (a-z, A-Z, 0-9, or an underscore), a period, or a hyphen. These are the most commonly used valid characters in the first part of an email address. The \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
  • The \ before the dash and period “escapes” these characters—that is, it indicates that the dash and period aren't a regex special characters themselves. There's no need to escape the period within the square brackets.
  • {0,25} indicates that from 0 to 25 characters in the preceding character set can occur before the @ symbol. The Content Compliance email setting supports matching of up to 25 characters for each character set in a regular expression.
  • The (...) formatting groups the domains, and the | character that separates them indicates an “or.”

Match any IP address in a range
Usage example Match any IP address within the range 192.168.1.0 to 192.168.1.255.
Regex examples Example 1:   192\.168\.1\.
Example 2:   192\.168\.1\.\d{1,3}
Notes
  • The \ before each period “escapes” the period—that is, it indicates that the period isn't a regex special character itself.
  • In Example 1, no characters follow the last period, so the regex matches any IP address beginning with 192.168.1., regardless of the number that follows.
  • In Example 2, \d matches any digit from 0 to 9 after the last period, and {1,3} indicates that the digits 1 to 3 can appear after that last period. In this case, the regex matches any complete IP address beginning with 192.168.1.. This regex also matches invalid IP addresses, such as 192.168.1.999.

Match an alphanumeric format
Usage example Match the purchase order numbers for your company. This number has various possible 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’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the number.
  • ^ matches the start of a new line. Allows the regex to match the number if it appears at the beginning of a line, with no characters before it.
  • $ matches the end of a line. Allows the regex to match the number if it appears at the end of a line, with no characters after it.
  • [#\-] matches a pound sign or a hyphen after the letters po, and {0,1} indicates that one of those characters can occur zero or one times. The \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
  • \s matches a space, and {0,1} indicates that a space can occur zero or one times.
  • \d matches any digit from 0 to 9, and {2} indicates that exactly 2 digits must appear in this position in the number.

 

Was this helpful?

How can we improve it?
Search
Clear search
Close search
Google apps
Main menu
13482857561428615548
true
Search Help Center
true
true
true
true
true
73010
false
false