DEV Community

Cover image for The Regular Expression (RegEx) Cheat Sheet you always wanted

The Regular Expression (RegEx) Cheat Sheet you always wanted

Tapajyoti Bose on August 14, 2022

I get it even though Regex is incredibly useful, it is extremely hard to master! This is a cheat sheet that provides the most common RegEx use case...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

Most useful image I ever downloaded:

Image description

Collapse
 
olawalemumeen2 profile image
OLAWALE MUMEEN

Abeg where you download this

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.)

Collapse
 
incrementis profile image
Akin C.

Hello Tapajyoti Bose,

thank you for your article.
I think I'll find it useful when I start working with regex again, as I'm no real expert.
I especially like how you added an example with a sample match for better understanding.

Collapse
 
paulknulst profile image
Paul Knulst

Thanks for sharing this list.
Normally, I never know how to regex. Just opening regexr.com and try different patterns.

Collapse
 
phlash profile image
Phil Ashby

Nice work, and of course the required XKCD: xkcd.com/208/

As a corollary, the inimitable Jeff Atwood: blog.codinghorror.com/regular-expr...

😄

Collapse
 
kasukur profile image
Sri

very well written with examples, thank you

Collapse
 
dodov profile image
Hristiyan Dodov

I like Regex 101 because it has all of that information right where you can write and test your regex.

Collapse
 
olawalemumeen2 profile image
OLAWALE MUMEEN

Thanks for providing this

Collapse
 
renancferro profile image
Renan Ferro

Woww, niice!

Collapse
 
wilmela profile image
Wilmela

Nice

Collapse
 
shshank profile image
Shshank

Nice one, thanks for sharing.

Collapse
 
sfritsch09 profile image
Sebastian Fritsch

BEST Cheatsheet for Regex: Regex Cheatsheet

Collapse
 
mroeling profile image
Mark Roeling

One of the most complete regex documentation sites, and one that I use for many years: regular-expressions.info/