DEV Community

Discussion on: I hated Regex so much that I made iHateRegex.io

Collapse
 
hyftar profile image
Simon Landry • Edited

Well simply put, Regex is not the right tool for validating email addresses and should be only used as feedback on the frontend of your app. If the patterns you're using get this complicated, it's probably a sign the thing you're using it for isn't the right task for the tool (and considering the amount of people in the comments of your website that are finding all sorts of edge cases, I think this is accurate).

Of course if you want to do things right, it often gets really tricky, but that's like showing the code base of a huge website as an example to someone who wants to learn a programming language, of course he's going to be overwhelmed and will feel defeated, but no one should start with the complicated stuff.

Regex at its core is fairly simple, but sadly, it's often overused and misused.

Thread Thread
 
geongeorge profile image
Geon George

Regex at its core is fairly simple, but sadly, it's often overused and misused.

That was one of the main reasons why I started this idea. It's a simple cheat sheet for commonly used regular expressions.

Also, I don't really hate regex (hypocrite me 😂). The name is just a funny thing I came up with. I've seen a lot of hate for this online.

I hope the idea became clear when you opened the website. It's not a rant about how many people and I hate the thing.

PS: If you ask me the only email validation expression that I might ever use with regex is this: ihateregex.io/expr/email

Thread Thread
 
hyftar profile image
Simon Landry

I completely agree with the email one, that's also the one I went with in most of my projects. I convinced some teachers in the past that Regex is not the best tool for email validation and that we should use that pattern in frontend instead.

Also I figured you didn't actually hate the thing if you created a whole website about the thing 😂😝.

You've done a good job of explaining it, although some patterns (such as the IP one) could probably be simplified a bit.

I'll definitely forward your website if I see someone struggling. Great job!

Thread Thread
 
geongeorge profile image
Geon George

Thank you. I need to improve the explanations as well. (Will fix those :) )

at some point, I really felt embarrassed to share this.
It's been sitting in my Github for a while and I finally decided to share and see what everyone else thinks about it.
Your feedback has been of great help :D (there is also a beginners tutorial article in the making)

Thread Thread
 
hyftar profile image
Simon Landry

If your repo is public, I'd be glad to help with a PR or two! 😊

Thread Thread
 
geongeorge profile image
Geon George

Here you go 😊:
github.com/geongeorge/i-hate-regex

The code is not the most elegant (I have to warn you)

Thread Thread
 
iamschulz profile image
Daniel Schulz • Edited

Regex is not the right tool for validating email addresses

Huh. I always do complex stuff like this with regex, in frontend as well as in backend.
What would be your tool of choice?

Thread Thread
 
hyftar profile image
Simon Landry • Edited

Depends on the language, in PHP for example, there's this library: github.com/egulias/EmailValidator

Basically, since there are so many rules and edge cases about emails, creating a Regex to solve that problem is not a productive approach since if new edge cases are created or found, maintenance instantly becomes hell and the pattern most likely has to be recreated from scratch.

Fortunately, people have already poured tons of hours into solving that problem for us and we can build on their shoulders.

Another example where regex looks like an appropriate tool but isn't is with parsing HTML. It sprung a famous Stack overflow question answer.

Thread Thread
 
geongeorge profile image
Geon George

I just launched iHateRegex on Producthunt 😺

👉👉
producthunt.com/posts/i-hate-regex