DEV Community

Cover image for Boost your Productiveness with RegEx (a little)

Boost your Productiveness with RegEx (a little)

Jan Wedel on July 09, 2019

I love RegEx, I use it every day and I will show you how to use it to easily get some smaller and larger tasks done. But... Don’t use i...
Collapse
 
stochastimus profile image
Larry Lancaster

Great article, good topic. If you’re an expert, there’s no reason not to add regexes to your bag of tricks. The key is to understand not only what happens logically, but also the runtime consequences. For example, take PCRE2, an ubiquitously available library. In this flavor of extended regex, you can use greedy matching (i.e., \d++). Used right, along with other constructs, you can judiciously avoid backtracking by the regex state machine and make your regexes fast and lean. I would advise not to be afraid of them, but like swords, to respect them and understand how to work with them. So it is often with powerful things. :)

Collapse
 
stealthmusic profile image
Jan Wedel

Thanks 🙏
Since I would not consider myself as an expert, I would not do it :)
I would still vote against if there is any more readable alternative. Strive for readability/maintainability and only optimize for speed if it’s necessary.

Collapse
 
stochastimus profile image
Larry Lancaster

Of course! Makes sense.

Collapse
 
skhmt profile image
Mike • Edited

As an alternative to RegEx Golf, I've found Regex Crossword to be pretty fun!

Collapse
 
kip13 profile image
kip

Good!

Collapse
 
stealthmusic profile image
Jan Wedel

Thanks, I will have a look!
Looks like you’ve started a markdown link but missed the url... ;)

Collapse
 
skhmt profile image
Mike
Collapse
 
jscooksey profile image
Justin Cooksey

I really have to learn more regex. I use the online tools to figure what I need, but I really need to learn more on it, so it's more ingrained. Especially on search and replace in editors.
Thanks for the article.

Collapse
 
aadibajpai profile image
Aadi Bajpai

I love regular expressions! I was able to circumvent using two whole different APIs by employing some very clever regex string manipulation in one of my projects. The speed improvement is unparalleled.

Collapse
 
stealthmusic profile image
Jan Wedel • Edited

It depends on the circumstances and requirements but I’d still reply with:
dev.to/stealthmusic/comment/cnm2

Collapse
 
aadibajpai profile image
Aadi Bajpai

I'd say that regex should be used if they can make a significant difference and you're aware of the scope of the problem being solved by it. That's where Cloudflare went wrong, I'd say. I use it for url formatting so even if it goes wrong, all I get is a 404 hopefully :P

Collapse
 
stealthmusic profile image
Jan Wedel

Thanks for your advise. I absolutely share your views, so I have to ask if you actually read the first section about „not to use it in production“? ;)
I even use the results of such an operation only for testing purposes.

Collapse
 
stealthmusic profile image
Jan Wedel

BTW, I just added a disclaimer, just in case what I wrote here could be misunderstood. I don’t mean something like „using regex is dangerous but I will show you how to do it right“. That’s absolutely not what I intended.

Collapse
 
rickmcgavin profile image
Rick McGavin

I read this, and fatefully was given the task of taking taking two excel columns of 6,000 zip codes and turning them in to arrays. Made incredibly quick work of that, so thanks!

Collapse
 
stealthmusic profile image
Jan Wedel

Haha, I‘m glad my article could help! 😊

Collapse
 
softmantk profile image
NIKHIL CM • Edited

Thanks @stealthmusic . It was very helpful. I tried it, and it is amazing !

Collapse
 
stealthmusic profile image
Jan Wedel

Glad it helps. There are certainly more things to explore and learn. :)

Collapse
 
cyr1l profile image
cyr1l

Nice article. Another alternative, an online visual regex tester: extendsclass.com/regex-tester.html