({{, (RefLink[RegularExpression, paclet:ref/RegularExpression])["regex"]
represents the generalized regular expression specified by the string "regex". }})

OpenersNotesSection(Spacer1)Details

(RefLink[RegularExpression, paclet:ref/RegularExpression]) can be used to represent classes of strings in functions like (RefLink[StringMatchQ, paclet:ref/StringMatchQ]), (RefLink[StringReplace, paclet:ref/StringReplace]), (RefLink[StringCases, paclet:ref/StringCases]), and (RefLink[StringSplit, paclet:ref/StringSplit]).

(RefLink[RegularExpression, paclet:ref/RegularExpression]) supports standard regular expression syntax of the kind used in typical string manipulation languages.

The following basic elements can be used in regular expression strings:

The following represent classes of characters:

The following named classes can be used: alnum, alpha, ascii, blank, cntrl, digit, graph, lower, print, punct, space, upper, word, xdigit.

The following represent positions in strings:

The following set options for all regular expression elements that follow them:

\\., \\[, etc. represent literal characters ., [, etc.

Analogs of named Wolfram Language patterns such as x:expr can be set up in regular expression strings using (regex).

Within a regular expression string, \\gn represents the substring matched by the n(^th) parenthesized regular expression object (regex). The shorter \\n is often equivalent to \\gn.

For the purpose of functions such as (RefLink[StringReplace, paclet:ref/StringReplace]) and (RefLink[StringCases, paclet:ref/StringCases]), any $n appearing in the right-hand side of a rule (RefLink[RegularExpression, paclet:ref/RegularExpression])["regex"]->rhs is taken to correspond to the substring matched by the n(^th) parenthesized regular expression object in regex. $0 represents the whole matched string.

OpenersPrimaryExamplesSection(Spacer1)Examples  (27)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Find words involving the characters a, b, c, d, e:

StringCases["adefgh12c34",RegularExpression["[a-e]+"]]

Equivalent form using string patterns:

StringCases["adefgh12c34",CharacterRange["a","e"]..]

Decide whether the string consists of words and whitespace:

StringMatchQ["abcd\nefgh\n1234",RegularExpression["(\\w|\\s)*"]]

Equivalent form using string patterns:

StringMatchQ["abcd\nefgh\n1234",(WordCharacter|Whitespace)...]

OpenersExampleSection1(Spacer1)Scope  (21)

OpenersExampleSubsection0(Spacer1)Basic Constructs  (17)

Extract any character except newline:

StringCases["a23b42c63d80, 123",RegularExpression["."]]

StringCases["a23b42c63d80, 123",Except["\n",_] ]

Either of the characters "a" and "b":

StringCases["a13b12c1da32efg", RegularExpression["[ab]"]]

StringCases["a13b12c1da32efg","a"|"b"]

Any character between "a" and "e", including "a" and "e":

StringCases["adefgh12c34",RegularExpression["[a-e]"]]

StringCases["adefgh12c34",CharacterRange["a","e"]]

Any character except "a" and "1":

StringCases["a13b12c17a32",RegularExpression["[^a1]"]]

StringCases["a13b12c17a32",Except["a"|"1",_]]

Any digit repeated one or more times:

StringCases["a23b4222c63333d80",RegularExpression["\\d+"]]

StringCases["a23b4222c63333d80",NumberString]

The character "a" repeated 2 or 3 times:

StringCases["aabc1aaaagh2ade", RegularExpression["a{2,3}"]]

StringCases["aabc1aaaagh2ade", w:(x_ ...) /; (2≤ StringLength[w]≤ 3) ]

Any digit:

StringCases["a2322c63333d80", RegularExpression["\\d"]]

StringCases["a2322c63333d80", DigitCharacter]

Nondigit characters:

StringCases["a2322c63333d80", RegularExpression["\\D"]]

StringCases["a2322c63333d80", Except[DigitCharacter]]

Space, newline, tab, or other whitespace character:

StringCases["13\na22\t bbb",RegularExpression["\\s"]]//InputForm

StringCases["13\na22\t bbb",WhitespaceCharacter]//InputForm

Non-whitespace characters:

StringCases["13\na22\t bbb",RegularExpression["\\S"]]

StringCases["13\na22\t bbb",Except[WhitespaceCharacter]]

Word characters:

StringCases["a23b42c63,d80", RegularExpression["\\w"]]

StringCases["a23b42c63,d80",WordCharacter]

Nonword characters:

StringCases["a23b:42c63;d80", RegularExpression["\\W"]]

StringCases["a23b:42c63;d80",Except[WordCharacter]]

Find all uppercase letters:

StringCases["AaBBccDDeefG",RegularExpression["[[:upper:]]+"]]

StringCases["AaBBccDDeefG",CharacterRange["A","Z"]..]

Split a string at the beginning of a new line:

StringSplit["line1\nline2\nline3",RegularExpression["(?m)^"]]//InputForm

StringSplit["line1\nline2\nline3",StartOfLine]//InputForm

Split a string at the end of a new line:

StringSplit["line1\nline2\nline3",RegularExpression["(?m)$"]]//InputForm

StringSplit["line1\nline2\nline3",EndOfLine]//InputForm

Insert a character at the boundary of each word:

StringReplace["123 45 6 789",RegularExpression["\\b"]:> "X"]

StringReplace["123 45 6 789",WordBoundary:> "X"]

Split a string at every character except at the boundary of a word:

StringSplit["12X X5X X89",RegularExpression["\\B"]]

StringSplit["12X X5X X89",Except[WordBoundary]]

OpenersExampleSubsection1(Spacer1)Compound Constructs  (4)

(RefLink[StringExpression, paclet:ref/StringExpression]) can contain (RefLink[RegularExpression, paclet:ref/RegularExpression]) objects:

StringCases["a13b12c17a32","a"~~x:RegularExpression["\\d+"]->x]

StringCases["a13b12c17a32","a"~~x:DigitCharacter..->x]

Conditional patterns:

StringCases["a23b42c63d80, 123",x:RegularExpression["\\d+"]/;Mod[ToExpression[x],2]==0]

StringCases["a23b42c63d80, 123",x:DigitCharacter../;Mod[ToExpression[x],2]==0]

Use alternatives to match one or more line breaks:

StringMatchQ["abcd\nefgh\n1234",RegularExpression["(.*|\\s*)*"]]

StringMatchQ["abcd\nefgh\n1234",(WordCharacter...|Whitespace)...]

Non-greedy matches are done by appending a question mark "?" to the quantifiers:

StringCases["abc1agh2cde",RegularExpression["a.+?\\d"]]

StringCases["abc1agh2cde",Shortest["a"~~__~~DigitCharacter]]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

The $1 refers to the letter matched by (.):

StringCases["aaabcccabbaacba",RegularExpression["(.)\\g1"]->"$1"]

StringCases["aaabcccabbaacba",x_~~x_->x]

Numbered subpatterns:

StringCases["a1b6a3b3a3c3a8b8",RegularExpression["(a(\\d))b\\g2"]->{"$0","$1","$2"}]

OpenersExampleSection3(Spacer1)Properties & Relations  (3)

Use (RefLink[StringMatchQ, paclet:ref/StringMatchQ]) to determine string pattern matches:

StringMatchQ["12345",RegularExpression["\\d+"]]

Use (RefLink[StringCases, paclet:ref/StringCases]) to find matching substrings:

StringCases["aaaa bbbb 1234",RegularExpression["[a-z]+"]]

Use (RefLink[StringSplit, paclet:ref/StringSplit]) to split a string into substrings using a delimiter pattern:

StringSplit["1.23, 4.56  7.89",RegularExpression["(\\s|,)+"]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[StringExpression, paclet:ref/StringExpression])  ▪  (RefLink[StringCases, paclet:ref/StringCases])  ▪  (RefLink[StringReplace, paclet:ref/StringReplace])  ▪  (RefLink[SearchQueryString, paclet:ref/SearchQueryString])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Regular Expressions, paclet:tutorial/RegularExpressions])}, {▪(RefLinkPlain[Special Characters, paclet:tutorial/SpecialCharacters-StringsAndCharacters])}, {▪(RefLinkPlain[String Patterns, paclet:tutorial/StringPatterns])}, {▪(RefLinkPlain[Working with String Patterns, paclet:tutorial/WorkingWithStringPatternsOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[String Patterns, paclet:guide/StringPatterns])}, {▪(RefLinkPlain[Programmable Linguistic Interface, paclet:guide/ProgrammableLinguisticInterface])}, {▪(RefLinkPlain[Text Content Types, paclet:guide/TextContentTypes])}, {▪(RefLinkPlain[Scientific Data Analysis, paclet:guide/ScientificDataAnalysis])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(Spacer1)▪(RefLinkPlain[(Spacer1)Implementation notes: Basic System Features, paclet:tutorial/SomeNotesOnInternalImplementation#16446])}, {(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])}})}})