WolframLang: RegularExpression vs StringExpression
RegularExpression vs StringExpression
On syntax, the
RegularExpression
syntax is more widely understood.
StringExpression
is more readable.
〔see WolframLang: 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
- 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