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... [Read Full]
markdown guide

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. :)


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.


I would advise against constructing JSON strings with regular expressions, as well as parsing CSV with them. These syntaxes are more convoluted (CSV can have quotes to escape commas in values, JSON is recursive, and rules of character escaping are different between these).

This sort of approach might be fine for a quick one-off job, but using it for more than that is asking for trouble.

It's just like this iconic Stack Overflow answer: stackoverflow.com/a/1732454


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.


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.


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.


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.


It depends on the circumstances and requirements but I’d still reply with:


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


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


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


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!


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

code of conduct - report abuse