RegularExpression
✖
RegularExpression
represents the generalized regular expression specified by the string "regex".
Details



- RegularExpression can be used to represent classes of strings in functions like StringMatchQ, StringReplace, StringCases, and StringSplit.
- 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:
-
c the literal character c . any character except newline [c1c2…] any of the characters ci [c1-c2] any character in the range c1–c2 [^c1c2…] any character except the ci p* p repeated zero or more times p+ p repeated one or more times p? zero or one occurrence of p p{m,n} p repeated between m and n times p*?,p+?,p?? the shortest consistent strings that match (p1p2…) strings matching the sequence p1, p2, … p1p2 strings matching p1 or p2 - The following represent classes of characters:
-
\\d digit 0–9 \\D nondigit \\s space, newline, tab, or other whitespace character \\S non-whitespace character \\w word character (letter, digit, or _) \\W nonword character [[:class:]] characters in a named class [^[:class:]] characters not in a named class - 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 beginning of the string (or line) $ the end of the string (or line) \\b word boundary \\B anywhere except a word boundary - The following set options for all regular expression elements that follow them:
-
(?i) treat uppercase and lowercase as equivalent (ignore case) (?m) make ^ and $ match start and end of lines (multiline mode) (?s) allow . to match newline (?-c) unset options - \\., \\[, 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
parenthesized regular expression object (regex). The shorter \\n is often equivalent to \\gn.
- For the purpose of functions such as StringReplace and StringCases, any $n appearing in the right‐hand side of a rule RegularExpression["regex"]->rhs is taken to correspond to the substring matched by the n
parenthesized regular expression object in regex. $0 represents the whole matched string.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Find words involving the characters a, b, c, d, e:

https://wolfram.com/xid/0i1pspc3m-bvws1x

Equivalent form using string patterns:

https://wolfram.com/xid/0i1pspc3m-c2nvnd

Decide whether the string consists of words and whitespace:

https://wolfram.com/xid/0i1pspc3m-hwo0ol

Equivalent form using string patterns:

https://wolfram.com/xid/0i1pspc3m-l8cuqv

Scope (22)Survey of the scope of standard use cases
Basic Constructs (17)
Extract any character except newline:

https://wolfram.com/xid/0i1pspc3m-wau8e2


https://wolfram.com/xid/0i1pspc3m-w4hydd

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

https://wolfram.com/xid/0i1pspc3m-d7r9kh


https://wolfram.com/xid/0i1pspc3m-i0jmqw

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

https://wolfram.com/xid/0i1pspc3m-o7hd44


https://wolfram.com/xid/0i1pspc3m-o92xxj

Any character except "a" and "1":

https://wolfram.com/xid/0i1pspc3m-5l58p4


https://wolfram.com/xid/0i1pspc3m-od63gz

Any digit repeated one or more times:

https://wolfram.com/xid/0i1pspc3m-jppyzw


https://wolfram.com/xid/0i1pspc3m-1muhv3

The character "a" repeated 2 or 3 times:

https://wolfram.com/xid/0i1pspc3m-qsi4wp


https://wolfram.com/xid/0i1pspc3m-qilu59


https://wolfram.com/xid/0i1pspc3m-o31a2j


https://wolfram.com/xid/0i1pspc3m-o282an


https://wolfram.com/xid/0i1pspc3m-wy0ncj


https://wolfram.com/xid/0i1pspc3m-7nbenu

Space, newline, tab, or other whitespace character:

https://wolfram.com/xid/0i1pspc3m-rf38ll


https://wolfram.com/xid/0i1pspc3m-r1rx6g


https://wolfram.com/xid/0i1pspc3m-24y6b7


https://wolfram.com/xid/0i1pspc3m-peqgki


https://wolfram.com/xid/0i1pspc3m-nz8lki


https://wolfram.com/xid/0i1pspc3m-ry18u0


https://wolfram.com/xid/0i1pspc3m-4126rn


https://wolfram.com/xid/0i1pspc3m-4a3hhu


https://wolfram.com/xid/0i1pspc3m-9n93cz


https://wolfram.com/xid/0i1pspc3m-gvrm1g

Split a string at the beginning of a new line:

https://wolfram.com/xid/0i1pspc3m-i3zsqg


https://wolfram.com/xid/0i1pspc3m-c0ui0d

Split a string at the end of a new line:

https://wolfram.com/xid/0i1pspc3m-tzmzng


https://wolfram.com/xid/0i1pspc3m-bbae3k

Insert a character at the boundary of each word:

https://wolfram.com/xid/0i1pspc3m-ofjaz0


https://wolfram.com/xid/0i1pspc3m-0pzjgd

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

https://wolfram.com/xid/0i1pspc3m-k60uey


https://wolfram.com/xid/0i1pspc3m-59e639

Compound Constructs (5)
StringExpression can contain RegularExpression objects:

https://wolfram.com/xid/0i1pspc3m-meg9qn


https://wolfram.com/xid/0i1pspc3m-cux7tm


https://wolfram.com/xid/0i1pspc3m-lsolo


https://wolfram.com/xid/0i1pspc3m-g4q96h

Use alternatives to match one or more line breaks:

https://wolfram.com/xid/0i1pspc3m-4q4jul


https://wolfram.com/xid/0i1pspc3m-ccppng

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

https://wolfram.com/xid/0i1pspc3m-czq8it


https://wolfram.com/xid/0i1pspc3m-iioam

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

https://wolfram.com/xid/0i1pspc3m-qbl9j2


https://wolfram.com/xid/0i1pspc3m-e17tgo


https://wolfram.com/xid/0i1pspc3m-jv6h6x


https://wolfram.com/xid/0i1pspc3m-xdpi2s

Properties & Relations (3)Properties of the function, and connections to other functions
Use StringMatchQ to determine string pattern matches:

https://wolfram.com/xid/0i1pspc3m-h2j60s

Use StringCases to find matching substrings:

https://wolfram.com/xid/0i1pspc3m-ld9gz9

Use StringSplit to split a string into substrings using a delimiter pattern:

https://wolfram.com/xid/0i1pspc3m-kqx6if

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