Regular expression examples

Examples of common regular expressions show you how you can set up regular expressions to find text patterns or to find patterns and replace parts of the returned strings. You can use these sample patterns and adapt them to your needs.

Matches regular expressions

The following table contains examples of regular expressions that can be used in match mode.
Table 1. Matches regular expression examples
Purpose Regular expression Sample text Sample match
Match a string of numbers of fixed length. \d{3} Contract Number 12-345 AB12 345
Match a string of any characters of a specific length. The string can consist of the characters a-z and the digits 0 - 9. \w{8} Contract Number 12-345 AB12 Contract
Match a string of any characters of a specific length at the beginning. The string can consist of the characters a - z and the digits 0 - 9. ^\w{6} Contract Number 12-345 AB12 Contra
Match a string of any two characters of fixed length followed by two digits. \w{2}\d{2} Contract Number 12-345 AB12 AB12
Match a word that is of fixed length with the assumption that the word is followed by a space. \w{8}\s Contract Number 12-345 AB12 Contract
Match a string of numbers of fixed length that also contains specific characters, for example, a contract number that consists of six characters with a hyphen after the second number. \d{2}-\d{3} Contract Number 12-345 AB12 12-345
Match a string of at least three numbers. \d{3,} Contract Number 12-345 AB12 345
Match the first folder in a path. ^([[:word:]]|\s)*(\\|\/) Folder 1\Folder 2\Folder 3\Folder 4 Folder 1

Replacement regular expressions

The following table shows examples of regular expressions that can be used in replacement mode.
Table 2. Replacement regular expression examples
Purpose Regular expression Replacement string Sample text Sample result
Get a folder path without a drive letter. ^[^\\]* $1 C:\folder 1\folder 2 \folder 1\folder 2
Get a drive letter from a folder path. (\\.*)   C:\folder 1\folder 2 C:
Get a specific folder in a path with a drive letter.

For each section in a path with a drive letter in it, repeat the expression:([^\\]*)\\? with the value:.* at the end of the expression.

Use $<section number> to get the specific level required, where section one is the drive letter, section two is the first folder in the path, section three is the second folder in the path, and so on.

([^\\]*)\\?.* $1 C:\one\two\three C:
([^\\]*)\\?([^\\]*)\\?.* $2 C:\one\two\three one
([^\\]*)\\?([^\\]*)\\?([^\\]*)\\?.* $3 C:\one\two\three two
Get the second folder in a path. ^[\\/]?[^\\/]+[\\/]([^\\/]+)([\\/][^\\/]+)* $1 Folder 1\Folder 2\Folder 3\Folder 4 Folder 2
Get the last two folders in a path. ((\\|\/)([[:word:]]|\s*)*){2}$ $1 Folder 1\Folder 2\Folder 3\Folder 4 Folder 3\Folder 4
Get all email with a case number matching the pattern "eight digits followed by a hyphen followed by three uppercase letters." Replace the case number with the phrase Automobile claim. (.*)(\d{8}-[A-Z]{3})(.*) $1Automobile claim$3 98765432-DEF, your email dated August 17, 2008 Automobile claim, your email dated August 17, 2008
Get all email with specific originator addresses and add the respective company name. (.?\Q@example.\E)(com|org|net) $1$2 (Example Company) Message forwarded by X@example.org Message forwarded by X@example.org (Example Company)
Search for IDs starting with AB and replace them with the department title Controlling. AB\w{2,4}@example\.com Controlling Sent by AB12@example.com Sent by Controlling
Remove any strings that match the pattern "four or more digits enclosed in parentheses." \(\d{4,}\)   Item number (12345) 6789 Item number 6789
Delete the forward prefix Fw: or the reply prefix Re: from the subject. ^(Fw:|Re:)(.*) $2 Fw: Your email regarding case number 98765432-DEF, dated August 17, 2008 Your email regarding case number 98765432-DEF, dated August 17, 2008
Truncate the value of the selected property to 80 characters, for example, the subject of an email. ^(.{0,80}).*$ $1 Network maintenance - Service interrupt Wednesday Feb. 6, 2008 - Monthly service interrupt February Network maintenance - Service interrupt Wednesday Feb. 6, 2008 - Monthly service