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
Β 
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
Β 
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
Β 
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
Β 
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
Β 
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
Β 
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

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! 😊