Regular Expressions

General Wolfram Language patterns provide a powerful way to do string manipulation. But particularly if you are familiar with specialized string manipulation languages, you may sometimes find it convenient to specify string patterns using regular expression notation. You can do this in the Wolfram Language with (RefLink[RegularExpression, paclet:ref/RegularExpression]) objects.

Using regular expression notation in the Wolfram Language.

This replaces all occurrences of a or b:

StringReplace["abcd acbd",RegularExpression["[ab]"]->"XX"]

This specifies the same operation using a general Wolfram Language string pattern:

StringReplace["abcd acbd","a"|"b"->"XX"]

You can mix regular expressions with general patterns:

StringReplace["abcd acbd",RegularExpression["[ab]"]~~_->"YY"]

(RefLink[RegularExpression, paclet:ref/RegularExpression]) in the Wolfram Language supports all standard regular expression constructs.

Basic constructs in Wolfram Language regular expressions.

This finds substrings that match the specified regular expression:

StringCases["abcddbbbacbbaa",RegularExpression["(a|bb)+"]]

This does the same operation with a general Wolfram Language string pattern:

StringCases["abcddbbbacbbaa",("a"|"bb")..]

There is a close correspondence between many regular expression constructs and basic general Wolfram Language string pattern constructs.

Correspondences between regular expression and general string pattern constructs.

Just as in general Wolfram Language string patterns, there are special notations in regular expressions for various common classes of characters. Note that you need to use double backslashes (∖∖) to enter most of these notations in Wolfram Language regular expression strings.

Regular expression notations for classes of characters.

This gives each occurrence of a followed by digit characters:

StringCases["a10b6a77a3a#",RegularExpression["a\\d+"]]

Here is the same thing done with a general Wolfram Language string pattern:

StringCases["a10b6a77a3a#","a"~~DigitCharacter..]

The Wolfram Language supports the standard POSIX character classes alnum, alpha, ascii, blank, cntrl, digit, graph, lower, print, punct, space, upper, word, and xdigit.

This finds runs of uppercase letters:

StringCases["AaBBccDDeefG",RegularExpression["[[:upper:]]+"]]

This does the same thing:

StringCases["AaBBccDDeefG",CharacterRange["A","Z"]..]

Regular expression notations for positions in strings.

In general Wolfram Language patterns, you can use constructs like x_ and x:patt to give arbitrary names to objects that are matched. In regular expressions, there is a way to do something somewhat like this using numbering: the n(^th) parenthesized pattern object (p) in a regular expression can be referred to as \\n within the body of the pattern, and $n outside it.

This finds pairs of identical letters that appear together:

StringCases["aaabcccabbaacba",RegularExpression["(.)\\1"]]

This does the same thing using a general Wolfram Language string pattern:

StringCases["aaabcccabbaacba",x_~~x_]

The $1 refers to the letter matched by (.):

StringCases["aaabcccabbaacba",RegularExpression["(.)\\1"]->"$1"]

Here is the Wolfram Language pattern version:

StringCases["aaabcccabbaacba",x_~~x_->x]