DEV Community

Discussion on: The Regular Expression (RegEx) Cheat Sheet you always wanted

Collapse
 
fjones profile image
FJones

As usual, I feel compelled to point out that you don't need most of these things and you don't want most of these things.

Especially readahead and lookbehind are features that add needless complexity and performance drawbacks (given an ideal implementation of the underlying algorithm).

My rule of thumb is usually: If you can't express it using exclusively concatenation, union, alternation, Kleene star, groups, and anchors, it probably shouldn't be done with regex.

This also ties into how I try to explain regex to people: A lot of the symbols are just shorthands for other symbol combinations:

a+ is the same as aa*. a? is the same as a|. Technically, even [ab] is just a|b. It's obviously a lot easier to write the shorthands - but the actual feature set you really need and want can all be boiled down into ^(|)*$ and that's wonderful. (Named and non-matching groups notwithstanding, of course.)