WolframLang: Regular Expression
What is Regular Expression
Regular Expression is a pattern for string.
Many string functions take a string pattern for argument.
The pattern should be one of:
RegularExpression[patternStr]
StringExpression[pattern]
〔see WolframLang: String Expression〕
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〕
Example RegularExpression
(* 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
- WolframLang: String
- WolframLang: String Functions
- WolframLang: Get SubString
- WolframLang: Convert String
- WolframLang: Format String
- WolframLang: String Match
- WolframLang: String Replace
- WolframLang: Regular Expression
- WolframLang: String Expression
- WolframLang: StringExpression Pattern Syntax
- WolframLang: RegularExpression vs StringExpression