2.8.5 Advanced Topic: Regular ExpressionsGeneral 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 "regex" |
Using regular expression notation in Mathematica. | This replaces all occurrences of a or b. | |
In[1]:=
StringReplace["abcd acbd", RegularExpression["[ab]"] -> "XX"]
|
Out[1]=
|
|
| This specifies the same operation using a general Mathematica string pattern. | |
In[2]:=
StringReplace["abcd acbd", "a" | "b" -> "XX"]
|
Out[2]=
|
|
| You can mix regular expressions with general patterns. | |
In[3]:=
StringReplace["abcd acbd", RegularExpression["[ab]"] ~~ _ -> "YY"]
|
Out[3]=
|
|
RegularExpression in Mathematica supports all standard regular expression constructs.
| c | the literal character c | | . | any character except newline | [ ... ] | any of the characters | [ - ] | any character in the range - | [^ ... ] | 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 | ( ... ) | strings matching the sequence , , ... | | | strings matching or |
Basic constructs in Mathematica regular expressions. | This finds substrings that match the specified regular expression. | |
In[4]:=
StringCases["abcddbbbacbbaa", RegularExpression["(a|bb)+"]]
|
Out[4]=
|
|
| This does the same operation with a general Mathematica string pattern. | |
In[5]:=
StringCases["abcddbbbacbbaa", ("a" | "bb") ..]
|
Out[5]=
|
|
There is a close correspondence between many regular expression constructs and basic general Mathematica string pattern constructs.
| . | _ (strictly Except["\n"]) | [ ... ] | Characters[" ... "] | [ - ] | CharacterRange[" ", " "] | [^ ... ] | Except[Characters[" ... "]] | | p* | p... | | p+ | p.. | | p? | p|"" | | p*?, p+?, p?? | ShortestMatch[p...], ... | ( ... ) | ( ~~ ~~ ... ) | | | | |
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.
| \\d | digit 0-9 (DigitCharacter) | | \\D | non-digit (Except[DigitCharacter]) | | \\s | space, newline, tab or other whitespace character (WhitespaceCharacter) | | \\S | non-whitespace character (Except[WhitespaceCharacter]) | | \\w | word character (letter, digit or _) (WordCharacter) | | \\W | non-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 a followed by digit characters. | |
In[6]:=
StringCases["a10b6a77a3a#", RegularExpression["a\\d+"]]
|
Out[6]=
|
|
| Here is the same thing done with a general Mathematica string pattern. | |
In[7]:=
StringCases["a10b6a77a3a#", "a" ~~ DigitCharacter ..]
|
Out[7]=
|
|
Mathematica supports the standard POSIX character classes alnum, alpha, ascii, blank, cntrl, digit, graph, lower, print, punct, space, upper, word, xdigit. | This finds runs of upper-case letters. | |
In[8]:=
StringCases["AaBBccDDeefG", RegularExpression["[[:upper:]]+"]]
|
Out[8]=
|
|
| This does the same thing. | |
In[9]:=
StringCases["AaBBccDDeefG", CharacterRange["A", "Z"] ..]
|
Out[9]=
|
|
| ^ | the beginning of the string (StartOfString) | | $ | the end of the string (EndOfString) | | \\b | word boundary (WordBoundary) | | \\B | anywhere except a word boundary (Except[WordBoundary]) |
Regular expression notations for positions in strings. In general Mathematica 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  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. | |
In[10]:=
StringCases["aaabcccabbaacba", RegularExpression["(.)\\1"]]
|
Out[10]=
|
|
| This does the same thing using a general Mathematica string pattern. | |
In[11]:=
StringCases["aaabcccabbaacba", x_ ~~ x_]
|
Out[11]=
|
|
| The $1 refers to the letter matched by (.). | |
In[12]:=
StringCases["aaabcccabbaacba", RegularExpression["(.)\\1"] -> "$1"]
|
Out[12]=
|
|
| Here is the Mathematica pattern version. | |
In[13]:=
StringCases["aaabcccabbaacba", x_ ~~ x_ -> x]
|
Out[13]=
|
|
|