Standard Regular Expression Strings
Regular expressions (RegEx) are a powerful way of matching a sequence of simple characters. You can use regular expressions in Forcepoint Email Security Cloud to create dictionary entries for lexical rules (see Filtering using lexical rules).
You can enclose a range of characters in square brackets to match against all of those characters. For example:
Expression | Description |
---|---|
[ ] | may also be used on a range of characters separated by a – character. |
[0-9] | matches any digit. |
[a-z] | matches any alpha character |
[a-z0-9] | matches any alphanumeric character |
^ |
is the “not” character, so [^0-9] matches against any character that is not a digit. |
Although you can use ranges to specify a group of characters, you can also use the following shortcuts:
Expression | Description |
---|---|
. | matches against any character |
\d | matches against a digit [0-9] |
\D | matches against a non-digit [^0-9] |
\s | matches against a whitespace character (such as a tab, space, or line feed character) |
\S | matches against a non-whitespace character |
\w | matches against an alphanumeric character [a-zA-Z_0-9] |
\W | matches against a non-alphanumeric character |
\xhh | matches against a control character (for the hexadecimal character hh) |
\uhhhh | matches against a Unicode character (for the hexadecimal character hhhh) |
To match against occurrences of a character or expression, you can use the following:
Expression | Description |
---|---|
* | matches against zero or more occurrences of the previous character or expression |
+ | matches against one or more occurrences of the previous character or expression |
? | matches zero or one occurrences of the previous character or expression |
{n} | matches n occurrences of the previous character or expression |
{n,m} | matches from n to m occurrences of the previous character or expression |
{n,} | matches at least n occurrences of the previous character or expression |
You can provide text to replace all or part of your search string. To do this, you need to group together matches by enclosing them in parentheses so they can be referenced in the replacement. To reference a matched parameter, use $n where n is the parameter starting from 1.
Regular expression examples
Example 1: IP address
The following regular expression matches against any IP address:
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
You can test this regex with any phrase including a standard IP address, for example 192.23.44.1.
Example 2: Dates
The following regular expression matches against dates in the format DD-MMMYYYY:
\b\d\d?-\w\w\w-\d\d\d\d\b
To test this regex, enter a sentence similar to “The project completes on 14-Feb-2009”.
Example 3: Social Security Numbers
The following regular expression matches against Social Security numbers in UK format:
\b\w{2}\d{6}\w\b
You can test this regex with any Social Security number in the format XY123456Z.