WolframLang: Regular Expression

By Xah Lee. Date: . Last updated: .

Many string functions take a string pattern for argument.

The pattern should be one of:


Represents a string pattern. Used in functions that take a string pattern.

Captured patterns can be represented by "$1" for first captured group, "$2" for second, etc, and "$0" represents the whole matched string. [see WolframLang: String Replace]


RegularExpression example

(* extract all digit sequence that start with b or c *)
StringCases["m144 c75 g927 c28 x12 b49",
 (* {c75, c28, b49} *)
(* extract email addresses *)
"some joe@example.com
 and jane@yymbqzxr.com",

(* {"joe@example.com", "jane@yymbqzxr.com"} *)

Ignore Case

Case sensitivity is usually specified by a option in the function. example: IgnoreCase -> True

(* match 2 literal char, ignore case *)
StringCases["some thing", RegularExpression["TH"], IgnoreCase -> True] ===

Replace String by Captured Patterns

WolframLang String