Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > String Manipulation > String Patterns >
Mathematica > Core Language > Rules & Patterns > Patterns > String Patterns >

StringExpression (~~)

Updated In 7 Graphic
s1~~s2~~... or StringExpression[s1, s2, ...]
represents a sequence of strings and symbolic string objects si.
  • "str1"~~"str2"~~... yields an ordinary string obtained by concatenating the characters in the "stri".
"string"a literal string of characters
_any single character
__any substring of one or more characters
___any substring of zero or more characters
x_, x__, x___ substrings given the name x
x:patternpattern 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 patti
patt/;conda pattern for which cond evaluates to True
pattern?testa pattern for which test yields True for each character
Whitespacea sequence of whitespace characters
NumberStringthe characters of a number
DatePattern[spec]the characters of a date
charobjan object representing a character class (see below)
RegularExpression["regexp"]substring matching a regular expression
StringExpression[...]an arbitrary string expression
  • The following represent classes of characters:
{c1,c2,...}any of the "ci"
Characters["c1c2..."]any of the "ci"
CharacterRange["c1","c2"]any character in the range "c1" to "c2"
HexadecimalCharacterhexadecimal digit 0-9, a-f, A-F
DigitCharacterdigit 0-9
LetterCharacterletter
WhitespaceCharacterspace, newline, tab or other whitespace character
WordCharacterletter or digit
Except[p]any character except ones matching p
  • The following represent positions in strings:
StartOfStringstart of the whole string
EndOfStringend of the whole string
StartOfLinestart of a line
EndOfLineend of a line
WordBoundaryboundary between word characters and others
Except[WordBoundary]anywhere except a word boundary
  • When constructs like __ or .. are present, there may be several different ways in which a given StringExpression can match a particular string.
  • By default, Mathematica will use the one that makes pattern elements that appear earlier in the StringExpression match the longest possible substrings.
  • The following determine which match will be used if there are several possibilities:
Shortest[p]the shortest consistent match for p
Longest[p]the longest consistent match for p (default)
  • In matching ordinary expressions instead of strings, the shortest instead of the longest consistent match is used.
A string expression representing the string "ab" followed by any single character:
This makes a replacement for each occurrence of the string pattern "ab"~~__:
Give all pairs of identical characters in the string:
A string expression representing the string "ab" followed by any single character:
In[1]:=
Click for copyable input
Out[1]=
This makes a replacement for each occurrence of the string pattern "ab"~~__:
In[2]:=
Click for copyable input
Out[2]=
 
Give all pairs of identical characters in the string:
In[1]:=
Click for copyable input
Out[1]=
A single blank (_) stands for any single character:
Double blank (__) stands for any sequence of one or more characters:
Triple blank (___) stands for any sequence of zero or more characters:
Find the primes with four identical digits:
By converting to strings, you can use StringMatchQ:
StringExpression objects can be used in many string manipulation functions:
New in 5.1 | Last modified in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team