String Patterns

An important feature of string manipulation functions like (RefLink[StringReplace, paclet:ref/StringReplace]) is that they handle not only literal strings but also patterns for collections of strings.

This replaces b or c by X:

StringReplace["abcd abcd","b"|"c"->"X"]

This replaces any character by u:

StringReplace["abcd abcd",_->"u"]

You can specify patterns for strings by using string expressions that contain ordinary strings mixed with Wolfram Language symbolic pattern objects.

String expressions.

Here is a string expression that represents the string ab followed by any single character:

"ab"~~_

This makes a replacement for each occurrence of the string pattern:

StringReplace["abc abcb abdc","ab"~~_->"X"]

Functions that support string patterns.

This gives all cases of the pattern that appear in the string:

StringCases["abc abcb abdc","ab"~~_]

This gives each character that appears after an "ab" string:

StringCases["abc abcb abdc","ab"~~x_->x]

This gives all pairs of identical characters in the string:

StringCases["abbcbccaabbabccaa",x_~~x_]

You can use all the standard Wolfram Language pattern objects in string patterns. Single blanks (_) always stand for single characters. Double blanks (__) stand for sequences of one or more characters.

Single blank (_) stands for any single character:

StringReplace[{"ab","abc","abcd"},"b"~~_->"X"]

Double blank (__) stands for any sequence of one or more characters:

StringReplace[{"ab","abc","abcd"},"b"~~__->"X"]

Triple blank (___) stands for any sequence of zero or more characters:

StringReplace[{"ab","abc","abcd"},"b"~~___->"X"]

Objects in string patterns.

This splits at either a colon or semicolon:

StringSplit["a:b;c:d",":"|";"]

This finds all runs containing only a or b:

StringCases["aababbcccdbaa",("a"|"b")..]

Alternatives can be given in lists in string patterns:

StringCases["aababbcccdbaa",{"a","b"}..]

You can use standard Wolfram Language constructs such as (RefLink[Characters, paclet:ref/Characters])["(Subscript[c, 1])(Subscript[c, 2])…"] and (RefLink[CharacterRange, paclet:ref/CharacterRange])["(Subscript[c, 1])","(Subscript[c, 2])"] to generate lists of alternative characters to use in string patterns.

This gives a list of characters:

Characters["aeiou"]

This replaces the vowel characters:

StringReplace["abcdefghijklm",Characters["aeiou"]->"X"]

This gives characters in the range "A" through "H":

CharacterRange["A","H"]

In addition to allowing explicit lists of characters, the Wolfram Language provides symbolic specifications for several common classes of possible characters in string patterns.

Specifications for classes of characters.

This picks out the digit characters in a string:

StringCases["a6;b23c456;",DigitCharacter]

This picks out all characters except digits:

StringCases["a6;b23c456;",Except[DigitCharacter]]

This picks out all runs of one or more digits:

StringCases["a6;b23c456",DigitCharacter..]

The results are strings:

InputForm[%]

This converts the strings to numbers:

ToExpression[%]+1

String patterns are often used as a way to extract structure from strings of textual data. Typically this works by having different parts of a string pattern match substrings that correspond to different parts of the structure.

This picks out each = followed by a number:

StringCases["a1=6.7, b2=8.87","="~~NumberString]

This gives the numbers alone:

StringCases["a1=6.7, b2=8.87","="~~x:NumberString->x]

This extracts "variables" and "values" from the string:

StringCases["a1=6.7, b2=8.87",v:WordCharacter..~~"="~~x:NumberString->{v,x}]

(RefLink[ToExpression, paclet:ref/ToExpression]) converts them to ordinary symbols and numbers:

ToExpression[%]^2

In many situations, textual data may contain sequences of spaces, newlines or tabs that should be considered "whitespace", and perhaps ignored. In the Wolfram Language, the symbol (RefLink[Whitespace, paclet:ref/Whitespace]) stands for any such sequence.

This removes all whitespace from the string:

StringReplace["aa b cc d",Whitespace->""]

This replaces each sequence of spaces by a single comma:

StringReplace["aa b cc d",Whitespace->","]

String patterns normally apply to substrings that appear at any position in a given string. Sometimes, however, it is convenient to specify that patterns can apply only to substrings at particular positions. You can do this by including symbols such as (RefLink[StartOfString, paclet:ref/StartOfString]) in your string patterns.

Constructs representing special positions in a string.

This replaces "a" wherever it appears in a string:

StringReplace[{"abc","baca"},"a"->"XX"]

This replaces "a" only when it immediately follows the start of a string:

StringReplace[{"abc","baca"},StartOfString~~"a"->"XX"]

This replaces all occurrences of the substring "the":

StringReplace["the others","the"->"XX"]

This replaces only occurrences that have a word boundary on both sides:

StringReplace["the others",WordBoundary~~"the"~~WordBoundary->"XX"]

String patterns allow the same kind of /; and other conditions as ordinary Wolfram Language patterns.

This gives cases of unequal successive characters in the string:

StringCases["aaabbcaaaabaaa",x_~~y_/;x!=y]

When you give an object such as x__ or e.. in a string pattern, the Wolfram Language normally assumes that you want this to match the longest possible sequence of characters. Sometimes, however, you may instead want to match the shortest possible sequence of characters. You can specify this using (RefLink[Shortest, paclet:ref/Shortest])[p].

Objects representing longest and shortest matches.

The string pattern by default matches the longest possible sequence of characters:

StringCases["-(a)--(bb)--(c)-","("~~__~~")"]

(RefLink[Shortest, paclet:ref/Shortest]) specifies that instead the shortest possible match should be found:

StringCases["-(a)--(bb)--(c)-",Shortest["("~~__~~")"]]

The Wolfram Language by default treats characters such "X" and "x" as distinct. But by setting the option (RefLink[IgnoreCase, paclet:ref/IgnoreCase])->(RefLink[True, paclet:ref/True]) in string manipulation operations, you can tell the Wolfram Language to treat all such uppercase and lowercase letters as equivalent.

Specifying case-independent string operations.

This replaces all occurrences of "the", independent of case:

StringReplace["The cat in the hat.","the"->"a",IgnoreCase->True]

In some string operations, one may have to specify whether to include overlaps between substrings. By default (RefLink[StringCases, paclet:ref/StringCases]) and (RefLink[StringCount, paclet:ref/StringCount]) do not include overlaps, but (RefLink[StringPosition, paclet:ref/StringPosition]) does.

This picks out pairs of successive characters, by default omitting overlaps:

StringCases["abcdefg",_~~_]

This includes the overlaps:

StringCases["abcdefg",_~~_,Overlaps->True]

(RefLink[StringPosition, paclet:ref/StringPosition]) includes overlaps by default:

StringPosition["abcdefg",_~~_]

Options for handling overlaps in strings.

This yields only a single match:

StringCases["abcd",__,Overlaps->False]

This yields a succession of overlapping matches:

StringCases["abcd",__,Overlaps->True]

This includes all possible overlapping matches:

StringCases["abcd",__,Overlaps->All]