DEV Community

Intro to Regex for Web Developers

Chris Achard on October 08, 2019

This was originally posted as a twitter thread: https://twitter.com/chrisachard/status/1181583499112976384 1. Regular expressions find ...
Collapse
 
danielvdbos profile image
danielvdbos

I have looked so hard and long the past year for an explanation of regex that I would understand and this is the first one that put everything I need to know together.

I really love the format of examples of how to use the theory you mention, so I can see how it will work, since most explanation either show the theory or just a bunch of examples.

Collapse
 
chrisachard profile image
Chris Achard

Glad you liked it! Yeah, it turns out regex is really complicated, heh... there are so many little rules - it's like its own little language all smushed together into a single line :)

Collapse
 
sarafian profile image
Alex Sarafian

Indeed the examples where the killers. You nailed it @chris

Collapse
 
maxwell_dev profile image
Max Antonucci

I recommend learning Regex if for no other reason than pulling off complex searches (and search and replaces) in code editors. A few Regex tricks can turn an otherwise tiresome "find, check, and replace in all the files" to an automated search and replace function. Great to see more content helpers coders get to that point!

Collapse
 
chrisachard profile image
Chris Achard

Yes! This is probably my number one use case as well; that, and trying to extract data from HTML or other messy data 😯 which I know, I know, isn't a good use case (I even say so in the post!) but for one-off tasks it's really powerful.

Collapse
 
metalcar profile image
MetalCar

I'm glad I found this one. Finally it's possible for everyone to understand regex :D I like your summary, Chris.

I can recommend this tool/website for testing and building regex. It helped me a lot: regex101.com/

Collapse
 
chrisachard profile image
Chris Achard

Great! Glad it helped.

That's a nice site - thanks for the link!

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

I like Regex since I tried to create a validator for checking my identity card in my country back in the day.

I think it is worth to mention that there are multiple variants of Regex implementations like the one in Perl, Linux or PHP I think.

Thus it depends on the developer. It might make sense to adopt certain variant of Regex to get what you need.

Collapse
 
chrisachard profile image
Chris Achard

Yes, that's a good point. The major points are the same across languages, but there are some differences (how the flags behave for example) - so if you're using the more advanced features, then watch out for cross language differences.

Collapse
 
nickytonline profile image
Nick Taylor • Edited

Great read Chris!

One resource I found super useful, once I dove into regexes more was the Mastering Regular Expressions book. I have the 2nd edition, but there appears to be a 3rd edition out.

Looking forward to your next post!

Collapse
 
mroeling profile image
Mark Roeling

Great building up of the level of the regex's!
I kinda started my regex knowledge from regular-expressions.info/, the guys that also made RegexBuddy. Very detailed and with a lot of simple examples.

And if you really want to get a feeling for regex's, try regexcrossword.com/.

Collapse
 
chrisachard profile image
Chris Achard

Thanks for the extra resources!

Collapse
 
simonholdorf profile image
Simon Holdorf

Nice one, Chris

Collapse
 
chrisachard profile image
Chris Achard

Thanks! While writing it I realized that regex is just really complicated 😅 I had trouble distilling it down into a single "intro" post.

I do have ideas about a page with real examples and explanations though that I think will help - so I'm working on that :)

Collapse
 
simonholdorf profile image
Simon Holdorf

Yeah I often have those ideas where I think "okay I am just going to make it easy and understandable" and then "why the heck has this to be so complicated" :)

Collapse
 
kavinda1995 profile image
Kavinda Jayakody

This is what i always wanted <3. Tried hard in the past to understand regex and this did the work!

Collapse
 
chrisachard profile image
Chris Achard

Great! Glad it helped 😄

Collapse
 
tapaibalazs profile image
Tápai Balázs

Thank you! I've learned a lot from this post. :)

Collapse
 
abdelrahman84 profile image
Abdelrahman Ahmed

Very nicely explained. First time to understand what Regex is about. Thanks.

Collapse
 
qkleinfelter_98 profile image
Quinn Kleinfelter

Just a note, under part 8, your escape sequence seems to have been escaped into just a single *, so its missing the backslash now.

Collapse
 
chrisachard profile image
Chris Achard

Oh, whoops! Good catch... turns out markdown will take \* as an escaped *, so I needed double backslash there. Thanks!

Collapse
 
codechunker profile image
codechunker

This is such an awesome post. Thank you so much. There is also one for Java dev.to/codechunker/introduction-to...

Collapse
 
icaropnbr profile image
icaropnbr

Nice job! Helped a lot!

;)

Collapse
 
jreckers profile image
jreckers

Really, really, nice explanations with examples!

Collapse
 
chrisachard profile image
Chris Achard

Thanks!

Collapse
 
orivolfo profile image
Ori Volfovitch

This is the best REGEX explanation + examples I have ever came across on the web!

Thank you!!

Collapse
 
chrisachard profile image
Chris Achard

Awesome! Glad to hear it 😃

Collapse
 
webdeasy profile image
webdeasy.de

Great summary for beginners, good job! 😇

Collapse
 
dlukanin profile image
Dmitry Lukanin

I am using regex for years and still im like "i have no idea what im doing" in it. Regex is really hard to master. Thank you.

Collapse
 
chrisachard profile image
Chris Achard

Yes, it is for sure :) I still am not good with lookaheads, etc... but thankfully regex is still really useful even with just the basics!

Collapse
 
oscar6echo profile image
oscar6echo

Very useful summary.

Collapse
 
adisreyaj profile image
Adithya Sreyaj

Regex has always been something which is hard to grasp(atleast for me)....
This post proves it to be wrong.

Excellent Post Chris.
Thanks✌️

Collapse
 
chrisachard profile image
Chris Achard

Glad it could help!

Yeah, it's often disheartening to see really complex regexes and get totally lost... but just a few basics can get you pretty far!

I still get lost in really complex regexes though 😂 especially when they start involving capturing groups or lookahead, etc.

Collapse
 
ceblakely profile image
Christina Blakely

This was the best intro guide to regex I could find, thank you!

Collapse
 
chrisachard profile image
Chris Achard

Awesome! Glad I could help 😃

Collapse
 
luis_ferreira profile image
Luís Ferreira

What a great post. I am saving this for the future and sharing it with my colleagues. Thank you!