DEV Community

Abdul Qadir Luqman
Abdul Qadir Luqman

Posted on

My RegExp nightmare

I seriously find regular expressions a boring area in programming. I sometimes think people who write good regular expressions have some kind of super powers. Am I alone here? 😐

Oldest comments (25)

Collapse
 
ben profile image
Ben Halpern

Not alone. I rarely turn to Regex unless it's the absolute obvious choice or it's the existing pattern in the code I'm modifying (and even then, sometimes there's a reason to remove it).

That being said, regex can be the underlying implementation of friendlier interfaces.

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

What do you use in place of regex?

Collapse
 
nektro profile image
Meghan (she/her)

You can implement a replaceAll function for Strings just using the built-in replace and regex:

String.prototype.replaceAll = function(search, replacement) {
    return this.replace(new RegExp(search, 'g'), replacement);
}
Collapse
 
nektro profile image
Meghan (she/her)

Not alone for sure. I don’t see anything “regular” in regular expressions and getting one that works can be very tough indeed. And don’t even get me started on flags haha

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

😂😂😂

Collapse
 
perigk profile image
Periklis Gkolias

I tend to avoid them. Not because I dont understand them(I believe I am above average there), but because I find them easy to lead me to dirty errors.

Usually there is a better alternative around the API of the language.

so give it a hard try but dont take it personally

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

APIs, I'll research more on that.

Collapse
 
elmuerte profile image
Michiel Hendriks

Yes we do have super powers. However, you do not have to get bitten by a radio active spider. Start small, and grow into larger expressions.
But, regex are no substitute to writing a proper parser. It is basically a rather powerful kludge.

Would you like to know more?

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

Thanks for the blog post link. Gives some useful tips on how to deal with this monster

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

Do you watch the silicon valley series? I'm asking because of your last name. Hendriks

Collapse
 
elmuerte profile image
Michiel Hendriks

Yes I do watch it. We're not related, I for one actually exist :p

Thread Thread
 
luqman10 profile image
Abdul Qadir Luqman

lol. But you can try and build the "new internet".

Collapse
 
pichardoj profile image
J. Pichardo

You are not alone, at all, however, I have found regular expressions to be incredibly powerful and extremely useful, it is always a good thing to know, or at least understand.

p.s. We do have super powers ;)

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

The importance and usefulness of regular expressions cannot be stressed enough. However, writing them can be daunting.

Collapse
 
pichardoj profile image
J. Pichardo

I find daunting to be fun. 😝

Collapse
 
tete0148 profile image
Théo

I love to write regular expressions, unfortunately I don't have enough occasions to use them in web development aside from form validation...

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

Yeah, I also mostly use it for validation.

Collapse
 
luqman10 profile image
Abdul Qadir Luqman

You must really be in love with regex

Collapse
 
sm0k3 profile image
Uladzislau Murashka

Hey, for better understanding and possibly more interesting learning process you can try such service like regexone.com/
This service helps you learn regular expressions with interesting tasks which you can solve by multiple ways :)

Collapse
 
mjb2kmn profile image
MN Mark

You're certainly not alone.

I actually do really enjoy regular expressions. It's one of the few things I can mentally dive into and not "come up for air" for hours. However, based on the developers I've met, I am in a very small minority.

Collapse
 
stealthmusic profile image
Jan Wedel • Edited

I love it. Usually I use in an text editor to create or modify large amounts of text. Sometimes I create sequences in Excel (yep, Excel) and then apply RegExes to it to build some JSON out of it. I rarely use it in production code, because readability and maintainability is much more important than short code.
Just today, we refactored a 3 line regEx to about 100 lines of code which is longer but much more comprehensible.