Wolfram: RegularExpression vs StringExpression
RegularExpression vs StringExpression
On syntax, the
RegularExpression
syntax is more widely understood.
StringExpression
is more readable.
〔see Wolfram: Regular Expression〕
They almost have the same power, except:
RegularExpression
allows you to have lookahead/lookbehind, and repeats of a given length.StringExpression
allows you to have condition test by expression (pattern/;expr
) and or conditions test by function (pattern?functionQ
), and orderless matchingAnyOrder[ pattern1, pattern2, pattern3 ]
. Also, you can embedRegularExpression
in it.

xx = "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green"; (* match string using regex, repace it by captured groups *) StringCases[xx, RegularExpression["(\\d+) (red|green|blue)"] :> {"$1","$2"} ] (* {{8, green}, {6, blue}, {20, red}, {5, blue}, {4, red}, {13, green}} *) (* same, using wolfram string expression syntax. *) StringCases[xx, d:NumberString~~" "~~c:"red"|"green"|"blue" :> {d,c} ] (* {{8, green}, {6, blue}, {20, red}, {5, blue}, {4, red}, {13, green}} *)
Reference
WolframLang String
- Wolfram: String
- Wolfram: String Functions
- Wolfram: Get SubString, by Index
- Wolfram: Get SubString, by Pattern
- Wolfram: Delete SubString
- Wolfram: String Split
- Wolfram: String Join
- Wolfram: String Insert
- Wolfram: Convert String
- Wolfram: String Template (format)
- Wolfram: String Match
- Wolfram: String Replace
- Wolfram: Regular Expression
- Wolfram: String Expression
- Wolfram: StringExpression Pattern Syntax
- Wolfram: RegularExpression vs StringExpression