MATHEMATICA TUTORIAL

# String Patterns

An important feature of string manipulation functions like StringReplace is that they handle not only literal strings but also patterns for collections of strings.

This replaces or by .
 Out[1]=
This replaces any character by .
 Out[2]=

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

 s1~~s2~~... or StringExpression[s1,s2,...] a sequence of strings and pattern objects

String expressions.

Here is a string expression that represents the string followed by any single character.
 Out[3]=
This makes a replacement for each occurrence of the string pattern.
 Out[4]=
 StringMatchQ["s",patt] test whether matches patt StringFreeQ["s",patt] test whether is free of substrings matching patt StringCases["s",patt] give a list of the substrings of that match patt StringCases["s",lhs->rhs] replace each case of lhs by rhs StringPosition["s",patt] give a list of the positions of substrings that match patt StringCount["s",patt] count how many substrings match patt StringReplace["s",lhs->rhs] replace every substring that matches lhs StringReplaceList["s",lhs->rhs] give a list of all ways of replacing lhs StringSplit["s",patt] split s at every substring that matches patt StringSplit["s",lhs->rhs] split at lhs, inserting rhs in its place

Functions that support string patterns.

This gives all cases of the pattern that appear in the string.
 Out[5]=
This gives each character that appears after an string.
 Out[6]=
This gives all pairs of identical characters in the string.
 Out[7]=

You can use all the standard Mathematica 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.
 Out[8]=
Double blank () stands for any sequence of one or more characters.
 Out[9]=
Triple blank () stands for any sequence of zero or more characters.
 Out[10]=
 "string" a literal string of characters _ any single character __ any sequence of one or more characters ___ any sequence of zero or more characters x_, x__, x___ substrings given the name x x:pattern pattern given the name x pattern.. pattern repeated one or more times pattern... pattern repeated zero or more times {patt1,patt2,...} or patt1|patt2|... a pattern matching at least one of the patt/;cond a pattern for which cond evaluates to True pattern?test a pattern for which test yields True for each character Whitespace a sequence of whitespace characters NumberString the characters of a number charobj an object representing a character class (see below) RegularExpression["regexp"] substring matching a regular expression

Objects in string patterns.

This splits at either a colon or semicolon.
 Out[11]=
This finds all runs containing only or .
 Out[12]=
Alternatives can be given in lists in string patterns.
 Out[13]=

You can use standard Mathematica constructs such as Characters["c1c2..."] and CharacterRange["c1", "c2"] to generate lists of alternative characters to use in string patterns.

This gives a list of characters.
 Out[14]=
This replaces the vowel characters.
 Out[15]=
This gives characters in the range through .
 Out[16]=

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

 {"c1","c2",...} any of the Characters["c1c2..."] any of the CharacterRange["c1","c2"] any character in the range to DigitCharacter digit 0-9 LetterCharacter letter WhitespaceCharacter space, newline, tab or other whitespace character WordCharacter letter or digit Except[p] any character except ones matching p

Specifications for classes of characters.

This picks out the digit characters in a string.
 Out[17]=
This picks out all characters except digits.
 Out[18]=
This picks out all runs of one or more digits.
 Out[19]=
The results are strings.
 Out[20]//InputForm=
This converts the strings to numbers.
 Out[21]=

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.
 Out[22]=
This gives the numbers alone.
 Out[23]=
This extracts "variables" and "values" from the string.
 Out[24]=
ToExpression converts them to ordinary symbols and numbers.
 Out[25]=

In many situations, textual data may contain sequences of spaces, newlines or tabs that should be considered "whitespace", and perhaps ignored. In Mathematica, the symbol Whitespace stands for any such sequence.

This removes all whitespace from the string.
 Out[26]=
This replaces each sequence of spaces by a single comma.
 Out[27]=

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 StartOfString in your string patterns.

 StartOfString start of the whole string EndOfString end of the whole string StartOfLine start of a line EndOfLine end of a line WordBoundary boundary between word characters and others Except[StartOfString], etc. anywhere except at the particular positions StartOfString, etc.

Constructs representing special positions in a string.

This replaces wherever it appears in a string.
 Out[28]=
This replaces only when it immediately follows the start of a string.
 Out[29]=
This replaces all occurrences of the substring .
 Out[30]=
This replaces only occurrences that have a word boundary on both sides.
 Out[31]=

String patterns allow the same kind of and other conditions as ordinary Mathematica patterns.

This gives cases of unequal successive characters in the string.
 Out[32]=

When you give an object such as or e.. in a string pattern, Mathematica 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 Shortest[p].

 Longest[p] the longest consistent match for p (default) Shortest[p] the shortest consistent match for p

Objects representing longest and shortest matches.

The string pattern by default matches the longest possible sequence of characters.
 Out[33]=
Shortest specifies that instead the shortest possible match should be found.
 Out[34]=

Mathematica by default treats characters such and as distinct. But by setting the option in string manipulation operations, you can tell Mathematica to treat all such uppercase and lowercase letters as equivalent.

 IgnoreCase->True treat uppercase and lowercase letters as equivalent

Specifying case-independent string operations.

This replaces all occurrences of , independent of case.
 Out[35]=

In some string operations, one may have to specify whether to include overlaps between substrings. By default StringCases and StringCount do not include overlaps, but StringPosition does.

This picks out pairs of successive characters, by default omitting overlaps.
 Out[36]=
This includes the overlaps.
 Out[37]=
StringPosition includes overlaps by default.
 Out[38]=
 Overlaps->All include all overlaps Overlaps->True include at most one overlap beginning at each position Overlaps->False exclude all overlaps

Options for handling overlaps in strings.

This yields only a single match.
 Out[39]=
This yields a succession of overlapping matches.
 Out[40]=
This includes all possible overlapping matches.
 Out[41]=

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »