DEV Community

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

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

The main problem with regex is that they overused.

For simple problems, regex are not needed, you have simpler, more readable solutions.

A combinaison of split(), subString(), removePrefix(), removeSuffix() is usually enough.

For complex problems, regex are not good at all.

Do not use a regex try to parse email, or URL, or HTML, ...

If the regex is not trivial, do not use a regex.

My advice:

Have a rule for pull requests that insists that every regex must come with a unit test that includes input that the regex is supposed to match and the ones that it's supposed to reject.

Collapse
 
geongeorge profile image
Geon George

I agree about the part where people shove in regex where it's absolutely overkill.

Collapse
 
geongeorge profile image
Geon George

Have a rule for pull requests that insists that every regex must come with a unit test that includes input that the regex is supposed to match and the ones that it's supposed to reject.

Will do <3

Collapse
 
mikeschinkel profile image
Mike Schinkel • Edited

For simple problems, regex are not needed, you have simpler, more readable solutions.

I used to feel the same.

Then I actually learned how to use regex.

Now I think that non-regex solutions that use more than one function are better done with regex. Because 80% of the time you only need simple regex patterns.

For complex problems, regex are not good at all.

(Almost) totally agree.