WolframLang: Regular Expression

By Xah Lee. Date: . Last updated: .

Many string functions take a string pattern for argument.

The pattern should be one of:

RegularExpression[patternStr]

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

RegularExpression example

(* extract all digit sequence that start with b or c *)
StringCases["m144 c75 g927 c28 x12 b49",
 RegularExpression["(b|c)\\d+"]]
 (* {c75, c28, b49} *)
(* extract email addresses *)
StringCases[
"some joe@example.com
 and jane@yymbqzxr.com",
RegularExpression["\\w+@\\w+\\.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] ===
{"th"}

Replace String by Captured Patterns

WolframLang String