Regular Expressions

General Mathematica 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 Mathematica with RegularExpression objects.

RegularExpression["regex"]a regular expression specified by

Using regular expression notation in Mathematica.

This replaces all occurrences of or .
In[1]:=
Click for copyable input
Out[1]=
This specifies the same operation using a general Mathematica string pattern.
In[2]:=
Click for copyable input
Out[2]=
You can mix regular expressions with general patterns.
In[3]:=
Click for copyable input
Out[3]=

RegularExpression in Mathematica supports all standard regular expression constructs.

cthe literal character c
.any character except newline
[c1c2...]any of the characters
[c1-c2]any character in the range -
[^c1c2...]any character except the
p*p repeated zero or more times
p+p repeated one or more times
p?zero or one occurrence of p
p{m,n}p repeated between m and n times
p*?, p+?, p??the shortest consistent strings that match
(p1p2...)strings matching the sequence
p1|p2strings matching or

Basic constructs in Mathematica regular expressions.

This finds substrings that match the specified regular expression.
In[4]:=
Click for copyable input
Out[4]=
This does the same operation with a general Mathematica string pattern.
In[5]:=
Click for copyable input
Out[5]=

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

._ (strictly Except["\n"])
[c1c2...]Characters["c1c2..."]
[c1-c2]CharacterRange["c1","c2"]
[^c1c2...]Except[Characters["c1c2..."]]
p*p...
p+p..
p?p|""
p*?, p+?, p??Shortest[p...],...
(p1p2...)(p1~~p2~~...)
p1|p2p1|p2

Correspondences between regular expression and general string pattern constructs.

Just as in general Mathematica 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 Mathematica regular expression strings.

\\ddigit 0-9 (DigitCharacter)
\\Dnon-digit (Except[DigitCharacter])
\\sspace, newline, tab, or other whitespace character (WhitespaceCharacter)
\\Snon-whitespace character (Except[WhitespaceCharacter])
\\wword character (letter, digit, or ) (WordCharacter)
\\Wnon-word character (Except[WordCharacter])
[[:class:]]characters in a named class
[^[:class:]]characters not in a named class

Regular expression notations for classes of characters.

This gives each occurrence of followed by digit characters.
In[6]:=
Click for copyable input
Out[6]=
Here is the same thing done with a general Mathematica string pattern.
In[7]:=
Click for copyable input
Out[7]=

Mathematica supports the standard POSIX character classes , , , , , , , , , , , , , and .

This finds runs of uppercase letters.
In[8]:=
Click for copyable input
Out[8]=
This does the same thing.
In[9]:=
Click for copyable input
Out[9]=
^the beginning of the string (StartOfString)
$the end of the string (EndOfString)
\\bword boundary (WordBoundary)
\\Banywhere except a word boundary (Except[WordBoundary])

Regular expression notations for positions in strings.

In general Mathematica patterns, you can use constructs like and 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 outside it.

This finds pairs of identical letters that appear together.
In[10]:=
Click for copyable input
Out[10]=
This does the same thing using a general Mathematica string pattern.
In[11]:=
Click for copyable input
Out[11]=
The refers to the letter matched by .
In[12]:=
Click for copyable input
Out[12]=
Here is the Mathematica pattern version.
In[13]:=
Click for copyable input
Out[13]=
New to Mathematica? Find your learning path »
Have a question? Ask support »