StringExpression
✖
StringExpression
represents a sequence of strings and symbolic string objects si.
Details




- "str1"~~"str2"~~… yields an ordinary string obtained by concatenating the characters in the "stri".
- The following objects can appear in StringExpression:
-
"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:pattern pattern given the name x pattern.. pattern repeated one or more times pattern... pattern repeated zero or more times {patt1,patt2,…} or patt1patt2… a pattern matching at least one of the patti AnyOrder[patt1, patt2, …] a pattern matching all of the patti in any order FixedOrder[patt1, patt2, …] a pattern matching the concatenation of the patti 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 DatePattern[spec] the characters of a date charobj an 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" HexadecimalCharacter hexadecimal digit 0-9, a-f, A-F 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 - The following represent positions in strings:
-
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[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, the Wolfram Language 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.
- StringExpression objects can be used in many string manipulation functions, including StringReplace, StringCases, StringSplit, and StringMatchQ.
- StringExpression has attributes Flat and OneIdentity.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
A string expression representing the string "ab" followed by any single character:

https://wolfram.com/xid/0enwfnclu-bveo8n

This makes a replacement for each occurrence of the string pattern "ab"~~__:

https://wolfram.com/xid/0enwfnclu-7ieuh3

Give all pairs of identical characters in the string:

https://wolfram.com/xid/0enwfnclu-3hq213

Scope (5)Survey of the scope of standard use cases
A single blank (_) stands for any single character:

https://wolfram.com/xid/0enwfnclu-g4bgre

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

https://wolfram.com/xid/0enwfnclu-ncfuy6

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

https://wolfram.com/xid/0enwfnclu-b9f5di

Use AnyOrder to match patterns in any order:

https://wolfram.com/xid/0enwfnclu-ed28t9

Assigning a matching AnyOrder to a variable preserves the order in the string:

https://wolfram.com/xid/0enwfnclu-j998vg

Applications (1)Sample problems that can be solved with this function
Find the primes with four identical digits:

https://wolfram.com/xid/0enwfnclu-cyghu7

By converting to strings, you can use StringMatchQ:

https://wolfram.com/xid/0enwfnclu-fe0f4p

Properties & Relations (1)Properties of the function, and connections to other functions
StringExpression objects can be used in many string manipulation functions:

https://wolfram.com/xid/0enwfnclu-dd8kms


https://wolfram.com/xid/0enwfnclu-d55aq2


https://wolfram.com/xid/0enwfnclu-i97mix


https://wolfram.com/xid/0enwfnclu-jszhv7


https://wolfram.com/xid/0enwfnclu-qhl02a


https://wolfram.com/xid/0enwfnclu-eoefc4


https://wolfram.com/xid/0enwfnclu-z8122m


https://wolfram.com/xid/0enwfnclu-ltlu86

Wolfram Research (2004), StringExpression, Wolfram Language function, https://reference.wolfram.com/language/ref/StringExpression.html (updated 2014).
Text
Wolfram Research (2004), StringExpression, Wolfram Language function, https://reference.wolfram.com/language/ref/StringExpression.html (updated 2014).
Wolfram Research (2004), StringExpression, Wolfram Language function, https://reference.wolfram.com/language/ref/StringExpression.html (updated 2014).
CMS
Wolfram Language. 2004. "StringExpression." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/StringExpression.html.
Wolfram Language. 2004. "StringExpression." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/StringExpression.html.
APA
Wolfram Language. (2004). StringExpression. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringExpression.html
Wolfram Language. (2004). StringExpression. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringExpression.html
BibTeX
@misc{reference.wolfram_2025_stringexpression, author="Wolfram Research", title="{StringExpression}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/StringExpression.html}", note=[Accessed: 25-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_stringexpression, organization={Wolfram Research}, title={StringExpression}, year={2014}, url={https://reference.wolfram.com/language/ref/StringExpression.html}, note=[Accessed: 25-March-2025
]}