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? 😐

Top comments (25)

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

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
 
luqman10 profile image
Abdul Qadir Luqman

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

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
 
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
 
bladefidz profile image
Hafidz Jazuli Luthfi

In my opinion, Regex is life-saving. For example This simple web scrapping job using regex at line 49. It save lot of loops.

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.

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
 
nancyd profile image
Nancy Deschenes

I find them useful and powerful. Maybe I have a superpower...

The way I approach a problem with regular expressions is to break it down into smaller parts, then build on what I have. Let's say I wanted to write a morse code parser. I would probably first try to validate that the input is valid morse code, so I start with a few strings that are valid, and a few that are not. I try my test strings against the regex I think will validate, and see if it works. Then I try replacing the letter E. I make sure that works. Then the letter T... I make sure my "._" doesn't get turned into "ET". Then I build on it...

What I hate about regexs is that they are "write only". That is, if a piece of code has a regular expression in it, you pretty much have to treat it like a black box. If you have to change it to handle a different case (you want to handle digits in your morse code translator) , you more or less have to recreate the regex from scratch. The current version is helpful for hints, but that's about it.

People who can look at a complicated regex and tell you what it does and what cases pass/dont are worthy of great admiration.

Wayne's World.

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
 
hoelzro profile image
Rob Hoelz

I remember feeling the same way when I was learning Perl - I was tempted to skip over the chapter on regexes, thinking I wouldn't use them. I didn't give in to that temptation; I buckled down and learned them.

And I am so glad I did!

I tend not to use regexes in long-lived code, but I use them all the time on the command line for adhoc data processing. I use them with perl oneliners to extract data, ack to search codebases, among other things. On top of this, I use patterns in Vim (Vim's own little regex dialect).

My advice would be to learn the basics - regexes are a really powerful tool to have in your toolbox!