DEV Community

Cover image for A Visual Guide to Regular Expression

A Visual Guide to Regular Expression

Amit Chaudhary on October 23, 2020

It's a common task in NLP to either check a text against a pattern or extract parts from the text that matches a certain pattern. A regular express...
Collapse
 
eecolor profile image
EECOLOR

Good write! When I explain regular expressions I tend to mention the following points:

  • Once you learn regular expressions, please remember that you barely ever need them. Things tend to look like regex nails ;-)
  • If your regular expression starts to look back at you in an intimidating fashion, you are probably better off using something else.
  • You should not use a regular expression for things that involve nesting (code, html, ...)

I also point people towards RegExr, especially the 'Tests' tab is amazing, make sure you also write tests for the things you do not want to match.

Collapse
 
amitness profile image
Amit Chaudhary

Great to know. I'll update the post with a link to Regexer.

Collapse
 
dougaws profile image
Doug

The greatest danger using regular expressions is a global search and replace. I can't tell you how many times I've seen inadvertent changes cause down stream problems.

Collapse
 
adamcoster profile image
Adam Coster

This is a super clear guide for getting started with regex! Nicely done.

Collapse
 
mroeling profile image
Mark Roeling • Edited

Well done! Next step: groups and backreferences :P

Addition for this writing:
? equals {0,1}
* equals {0,}
+ equals {1,}

All info with similar visualisations on regular-expressions.info/ .

Collapse
 
amitness profile image
Amit Chaudhary

Didn't know about this. It looks super helpful. Thank you for sharing.

Collapse
 
q2apro profile image
q2apro

Great tutorial. Also the highlighting of found matches is extremely helpful. 👍

I have learned Regex with regex101.com/ - I cannot recommend this tool enough. Helped me to master Regex. Before I was a complete noob and could not understand a single expression.

Collapse
 
dougaws profile image
Doug

I wrote a simple Windows forms utility where I can test REs. One text box for the RE I'm testing, one where I start typing in text and the utility responds with an indication of a match as I type each character.

Collapse
 
mbaas2 profile image
Michael Baas

Duh - how embarrassing is that: I've used regex for years - and only now have I learned about \D as a negation of \d! THANKS! (Can't wait to read in full to see what else I missed...)

Collapse
 
kmonsoor profile image
Khaled Monsoor • Edited

hey @amitness cool guide for difficult stuff like RegEx. 👏

I think you got a typo in the 2nd last example.
Why should "r'batman'" in

match = re.match(r'batman', 'The batman is cool')

should match only match for the occurrence at the beginning?

Collapse
 
ardunster profile image
Anna R Dunster

re.match() and re.search() work differently: stackoverflow.com/questions/180986...

Collapse
 
tiesmaster profile image
Thijs Brobbel

Awesome how you created this guide. It's so pretty!! How do you make these diagrams?

Collapse
 
amitness profile image
Amit Chaudhary

Thank you.

I created the diagrams using excalidraw.com

Collapse
 
dotorimook profile image
dotorimook

Good post. Nice to read.
Could you go further explaining capturing group with ()?

Collapse
 
josiasaurel profile image
Josias Aurel

Nice post on Regex on python. Your explanations are so good. Awesome.

Collapse
 
aucacoyan profile image
AucaCoyan

This is a really cool reference, thanks!

Collapse
 
yurieastwood profile image
Yuri Eastwood

Nice guide! Well done and visually appealing!

Collapse
 
evoratec profile image
Juan Antonio Navarro

Great. Thank you.

Collapse
 
sagar profile image
Sagar

This is one of the simplest explanations of Regular expression ever I see. I really enjoyed reading this article even I know a little bit of regex. Appreciate your efforts 😊.

Collapse
 
candidodmv profile image
Vinicius Dutra

Thank you Amit. You have made a great job, awaiting for the next chapters!

Collapse
 
willmvs profile image
William Sousa

Thank you for this guide Amit!

Collapse
 
sagartyagi121 profile image
Gajender Tyagi

@amitness very clear guide to regex. Thanks for this.

Collapse
 
qq449245884 profile image
qq449245884

Hello, may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.

Collapse
 
amitness profile image
Amit Chaudhary

Hey, sure.

Collapse
 
shaikh profile image
Javed Shaikh

Great post

Collapse
 
dougaws profile image
Doug

You need to change the title to something like "Using Basic Regular Expressions in Python" and tells us right in the beginning on which version of Python and the re package you tested your code.

Collapse
 
amitness profile image
Amit Chaudhary

Thank you for the suggestion. I will do that.

Collapse
 
truthseekerbeast profile image
Tapan Parmar

Pretty neat! Loved it, This is the first time someone explained Regex so well, Can I republish this content on my blog with proper mention of your profiles and original links?