Why Regular Expressions Are Super Powerful, But A Terrible Coding Decision
Tags: programming, watercooler, development, performance
Published: 2021-05-14 20:40:33

<div class="modal-body">
<p>We’ve all been there. You have a string input and need a fast and efficient way to parse something important out of it. Your options are relatively low since manually parsing a string for a pattern is tedious and often very inefficient as the string gets larger. So what do you do? You turn to Regular Expressions! What’s the problem with that? Let’s&nbsp;explore.</p>
<h3><strong>Impossible To Read or&nbsp;Debug</strong></h3>
<p>A huge assumption that is made when creating regular expressions is that the schema you are programming to won’t change. If it does, it could require rewriting the regular expression to hopefully produce the same usable output. But let’s say you are tasked with fixing a broken regular expression that fell victim to a changing schema. It means that you would have to first understand how the regex worked with the old schema, before understanding how the new schema changed. Only then can you rewrite the regular expression to account for the new input. That’s a fairly tedious process that is potentially very error prone. And the level of difficulty goes up exponentially with the length and complexity of regular expressions. I would hate to be the only one in charge of fixing this<a href="http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html"> 6.2kb monster that validates RFC822 email addresses</a>.</p>
<h3><strong>Regex Abuse</strong></h3>
<p>A common use case for regular expression is something like the following:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/555/0*IS5fzV7OCtIXAwsR"></figure><p>This regular expression tries to emulate a parser to rip out useful information into named capture groups from a structured data set like <a href="https://www.json.org/json-en.html">json</a>. The benefit of this is that (in c# at least) you then can have reference to exactly what the regular expression matched&nbsp;on.</p>
<p>The downside of this is that you are using the wrong tool for the job. As much as it might seem like a quick and easy solution, it causes more problems than it solves. Parsing json, xml, or even html with regular expressions is a terrible idea. And it’s mostly a solved problem. <a href="https://docs.python.org/3/library/html.parser.html">Check out this HTML Python Parser</a>. Using a tool like this will make your coding easier and make code maintenance easier in the&nbsp;future.</p>
<h3><strong>Balancing Act</strong></h3>
<p>I know most of this article has been bashing the use of regular expressions, but there are some benefits to using them (if used correctly). <strong>All developers and engineers should learn to use basic regular expressions, because they’ll produce better, more flexible, more maintainable code with them.</strong> When used responsibly, regular expressions are a huge net positive. For example, writing a regular expression to validate a phone number is relatively straightforward:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*yClX3Avq7-2wvB3T"></figure><h3><strong>Conclusion</strong></h3>
<p>Regular expressions are extremely powerful and useful in the right situation. When abused and used in incorrect situations, they can lead to ugly and unmaintainable code. So use them&nbsp;wisely!</p>
<p>Comment below your opinion on Regular Expressions and if you use them regularly!</p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=e4cd37fe957f" width="1" height="1" alt="">
</div>