({{, (Subscript[s, 1])~~(Subscript[s, 2])~~… or (RefLink[StringExpression, paclet:ref/StringExpression])[(Subscript[s, 1]),(Subscript[s, 2]),…]
represents a sequence of strings and symbolic string objects (Subscript[s, i]). }})

OpenersNotesSection(Spacer1)Details

"(Subscript[str, 1])"~~"(Subscript[str, 2])"~~… yields an ordinary string obtained by concatenating the characters in the "(Subscript[str, i])".

The following objects can appear in (RefLink[StringExpression, paclet:ref/StringExpression]):

The following represent classes of characters:

The following represent positions in strings:

When constructs like __ or .. are present, there may be several different ways in which a given (RefLink[StringExpression, paclet:ref/StringExpression]) can match a particular string.

By default, the Wolfram Language will use the one that makes pattern elements that appear earlier in the (RefLink[StringExpression, paclet:ref/StringExpression]) match the longest possible substrings.

The following determine which match will be used if there are several possibilities:

In matching ordinary expressions instead of strings, the shortest instead of the longest consistent match is used.

(RefLink[StringExpression, paclet:ref/StringExpression]) objects can be used in many string manipulation functions, including (RefLink[StringReplace, paclet:ref/StringReplace]), (RefLink[StringCases, paclet:ref/StringCases]), (RefLink[StringSplit, paclet:ref/StringSplit]), and (RefLink[StringMatchQ, paclet:ref/StringMatchQ]).

(RefLink[StringExpression, paclet:ref/StringExpression]) has attributes (RefLink[Flat, paclet:ref/Flat]) and (RefLink[OneIdentity, paclet:ref/OneIdentity]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

A string expression representing the string "ab" followed by any single character:

"ab" ~~ _

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

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

Give all pairs of identical characters in the string:

StringCases["abbcbccaabbabccaa",x_~~x_]

OpenersExampleSection1(Spacer1)Scope  (5)

A 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"]

Use (RefLink[AnyOrder, paclet:ref/AnyOrder]) to match patterns in any order:

StringMatchQ["4321", AnyOrder["1","2","3","4"]]

Assigning a matching (RefLink[AnyOrder, paclet:ref/AnyOrder]) to a variable preserves the order in the string:

StringCases["1342 1432", a:AnyOrder["1","2","3","4"]:>a]

OpenersExampleSection2(Spacer1)Applications  (1)

Find the primes with four identical digits:

FromDigits/@Select[IntegerDigits/@Array[Prime,1000,1000000],MatchQ[#,{___,x_,___,x_,___,x_,___,x_,___}]&]//Timing

By converting to strings, you can use (RefLink[StringMatchQ, paclet:ref/StringMatchQ]):

FromDigits/@Select[IntegerDigits/@Array[Prime,1000,1000000],StringMatchQ[FromCharacterCode[48+#],StringExpression[___,x_,___,x_,___,x_,___,x_,___]]&]//Timing

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

(RefLink[StringExpression, paclet:ref/StringExpression]) objects can be used in many string manipulation functions:

StringMatchQ["apppbb","a"~~___~~"b"]

StringFreeQ["bcde","c"~~__~~"t"]

StringCount["abcadcadcbaac","a"~~_~~"c"]

StringCases["a10b6a77a3a#","a"~~DigitCharacter..]

StringReplace["aa abcd",WordBoundary~~"aa"~~WordBoundary->"XX"]

StringReplaceList["abxxabxxabxxabxxab",_~~"ab"~~_->"X"]

StringPosition["abxxabxxabxxabxxab",_~~"ab"~~_]

StringReplacePart["abxxabxxabxxabxxab","X",%]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[RegularExpression, paclet:ref/RegularExpression])  ▪  (RefLink[String, paclet:ref/String])  ▪  (RefLink[StringJoin, paclet:ref/StringJoin])  ▪  (RefLink[StringReplace, paclet:ref/StringReplace])  ▪  (RefLink[StringCases, paclet:ref/StringCases])  ▪  (RefLink[Characters, paclet:ref/Characters])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[String Patterns, paclet:tutorial/StringPatterns])}, {▪(RefLinkPlain[Working with String Patterns, paclet:tutorial/WorkingWithStringPatternsOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[String Patterns, paclet:guide/StringPatterns])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[Patterns, paclet:guide/Patterns])}, {▪(RefLinkPlain[Programmable Linguistic Interface, paclet:guide/ProgrammableLinguisticInterface])}, {▪(RefLinkPlain[Language Overview, paclet:guide/LanguageOverview])}, {▪(RefLinkPlain[Text Content Types, paclet:guide/TextContentTypes])}, {▪(RefLinkPlain[Wolfram Language Syntax, paclet:guide/Syntax])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])(WebLink[: String Patterns and Templates , https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])}})}})