# 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.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

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

s_{1}~~s_{2}~~… or StringExpression[s_{1},s_{2},…] | |

a sequence of strings and pattern objects |

In[3]:= |

Out[3]= |

In[4]:= |

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.

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

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.

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

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 |

{patt_{1},patt_{2},…} or patt_{1}patt_{2}… | 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 |

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

You can use standard Wolfram Language constructs such as Characters["c_{1}c_{2}…"] and CharacterRange["c_{1}","c_{2}"] to generate lists of alternative characters to use in string patterns.

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

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

{"c_{1}","c_{2}",…} | any of the |

Characters["c_{1}c_{2}…"] | any of the |

CharacterRange["c_{1}","c_{2}"] | 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.

In[17]:= |

Out[17]= |

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |

In[20]:= |

Out[20]//InputForm= | |

In[21]:= |

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.

In[22]:= |

Out[22]= |

In[23]:= |

Out[23]= |

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

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 Whitespace stands for any such sequence.

In[26]:= |

Out[26]= |

In[27]:= |

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.

In[28]:= |

Out[28]= |

In[29]:= |

Out[29]= |

In[30]:= |

Out[30]= |

In[31]:= |

Out[31]= |

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

In[32]:= |

Out[32]= |

When you give an object such as 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 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.

In[33]:= |

Out[33]= |

In[34]:= |

Out[34]= |

The Wolfram Language by default treats characters such and as distinct. But by setting the option IgnoreCase->True in string manipulation operations, you can tell the Wolfram Language to treat all such uppercase and lowercase letters as equivalent.

IgnoreCase->True | treat uppercase and lowercase letters as equivalent |

Specifying case‐independent string operations.

In[35]:= |

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.

In[36]:= |

Out[36]= |

In[37]:= |

Out[37]= |

In[38]:= |

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.

In[39]:= |

Out[39]= |

In[40]:= |

Out[40]= |

In[41]:= |

Out[41]= |