Regular expression examples - Google Workspace Admin Help (2023)

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

  • Exact phrase match only
  • Combine words or phrases in a list
  • Combine words with different spellings or special characters
  • Match any email address from a specific domain
  • Matches any IP address in a range
  • Match an alphanumeric format

For additional instructions and guidelines, see alsoGuidelines for using regular expressionsyRE2 Syntax. See tooConfigure rules for content compliance.

Important:we supportRE2 Syntaxonly, it differs slightly from PCRE. Regular expressions are case sensitive by default.

Use:The examples below can be useful as starting points for more complex regular expressions. However, to match a single word, we suggest you use thecontent complianceoobjectionable contentDefinitions.

Exact phrase match only
Example of use Combine a frasestock tips.
Examples of regular expressions Example 1:(\W|^)estoque\stips(\W|$)

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

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

grades
  • \W matches any character that is not a letter, digit, or underscore. Prevents the regular expression from matching characters before or after the phrase.
  • In example 2, \s matches a space character and {0,3} indicates that 0 to 3 spaces can appear between wordsvaluesybribe.
  • ^ matches the beginning of a new line. Allows the regular expression to match the phrase if it appears at the beginning of a line, with no preceding characters.
  • $ matches the end of a line. Allows the regular expression to match the phrase if it appears at the end of a line, with no characters following it.
  • In example 3, (s) corresponds to the letters, and {0,1} indicates that the letter can appear0o1times after the word advice. So the regular expression matchesstock boardystock tips. Alternatively, you can use the ? instead of {0,1}
Combine words or phrases in a list
Example of use Combine any word or phrase from the following list:
  • camel
  • damn you
  • Shit
  • Fooey
  • damn damn
  • inferno
Regular Expression Example (?i)(\W|^)(nonsense|damn|merda|fooey|OMG\damn|heck)(\W|$)
grades
  • (...) groups all words together, so that the \W character class applies to all words in parentheses.

  • (?i) causes the content to match case.

  • \W matches any character that is not a letter, digit, or underscore. Prevents the regular expression from matching characters before or after words or phrases in the list.

  • ^ matches the beginning of a new line. Allows the regular expression to match the word if it appears at the beginning of a line, with no preceding characters.

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

  • | indicates an "or", then the regular expression matches any of the words in the list.

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

Combine words with different spellings or special characters
Example of use Combine the word viagra with some of the obfuscations spammers use, such as:
  • vi@gra
  • v1agra
  • v1@game
  • v!@gr@
Regular Expression Example v[i!1][a@]gr[a@]
grades
  • \W is not included, so other characters may appear before or after any of the variants ofViagra. For example, the regular expression still matchesViagrain the following text:

viagra!!o*** viagra ***

  • [i!1] matches characterseu,!, o1in the position of the second character of the word.
Match any email address from a specific domain
Example of use Match any domains email addressyahoo.com,hotmail.com, yougmail.com.
Regular Expression Example (\W|^)[\w.\-]{0,25}@(yahoo|hotmail|gmail)\.com(\W|$)
grades
  • \W matches any character that is not a letter, digit, or underscore. Prevents the regular expression from matching characters before or after the email address.
  • ^ matches the beginning of a new line. Allows the regular expression to match the address if it appears at the beginning of a line, with no preceding characters.
  • $ matches the end of a line. Allows the regular expression 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 \- (indicating a hyphen) must appear last in the list of characters enclosed in square brackets.
  • The \ before the hyphen and dot "escapes" these characters, that is, it indicates that the hyphen and dot are not special regular expression characters. There is no need to escape the dot inside the square brackets.
  • {0,25} indicates that 0 to 25 characters in the above character set can appear before the @ symbol. HimContent compliance email settingssupports matches of up to 25 characters for each character set in a regular expression.
  • The format (...) groups the domains and the | the character that separates them indicates an “o”.
Matches any IP address in a range
Example of use Matches any IP address within range192.168.1.0an192.168.1.255.
Examples of regular expressions Example 1:192\.168\.1\.
Example 2:192\.168\.1\.\d{1,3}
grades
  • The \ before each dot "escapes" the dot, that is, it indicates that the dot is not properly a regular expression special character.
  • In Example 1, no characters follow the last dot, so the regular expression matches any IP address starting with192.168.1., regardless of the number that follows.
  • In Example 2, \d matches any digit in0an9after the last period and {1,3} indicates that the digits 1 to 3 can appear after that last period. In this case, the regular expression matches any complete IP address starting with192.168.1.. This regular expression also matches invalid IP addresses such as192.168.1.999.
Match an alphanumeric format
Example of use Combine your company's purchase order numbers. This number has several possible formats, such as:
  • PO nn-nnnn
  • PO-nn-nnnn
  • PO # nn nnnn
  • PO#nn-nnnn
  • PO nnnnnn
Regular Expression Example (\W|^)po[#\-]{0,1}\s{0,1}\d{2}[\s-]{0,1}\d{4}(\W|$)
grades
  • \W matches any character that is not a letter, digit, or underscore. Prevents the regular expression from matching characters before or after the number.
  • ^ matches the beginning of a new line. Allows the regular expression to match the number if it appears at the beginning of a line, with no preceding characters.
  • $ matches the end of a line. Allows the regular expression to match the number if it appears at the end of a line, with no characters after it.
  • [#\-] matches a pound sign or hyphen after the letters po and {0,1} indicates that one of these characters can occur zero or one time. The \- (indicating a hyphen) must appear last in the list of characters enclosed in square brackets.
  • \s matches a space and {0,1} indicates that a space can occur zero or once.
  • \d matches any digit from 0 to 9 and {2} indicates that exactly 2 digits must appear at this position in the number.

Was it helpful?

How can we improve?

Need more help?

Sign up for additional support options to quickly resolve your issue

Check-in

Start your 14-day free trial today

Professional email, online storage, shared calendars, video conferencing and more.Start your free Google Workspace trial today.

Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated: 12/02/2022

Views: 6076

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.