Should You Learn Regex?

Should You Learn Regex?

Oftentimes you are tasked with checking for patterns in strings, validating text that conforms to certain patterns, and parsing data from text, and too often to count, are you baffled by how to perform such a task without brute forcing.

The answer to all your problems is a concept known as Regex (Regular Expressions). However, I can almost guarantee you've never heard of it - why? as Colt Steele mentioned in his course "The Ultimate Python Bootcamp", almost no programming course covers it in their curriculum despite its advantages and usefulness.

So this leads me to the overarching question of this article - should you learn Regex? I'll be putting this to test in three different ways.

Table of Contents

Use Cases

  • Email validation: you can easily check if an email is valid, which can be useful when trying to get a mailing list for your personal website, trying to collect emails for an event, etc.
  • Search engines: finding certain criteria and matching that to the user input requires Regex (for example, Google supports Regex input if the user needs very specific search results)
  • Find & replace: going through large amounts of data in a .csv file or trying to scrape through JSON data can be a nightmare, which is why Regex can be used to find and replace certain data within a file.

Why People Stay Away from Regex

As you saw above, even though Regex has a plethora of use cases, Regex can be daunting to some people.

Regex can be hard to learn, especially with so many commands. It can also be hard to decipher in code, especially due to its long string of unintelligible pairs of characters (I mean, look at this: ^[^@ ]+@[^@ ]+\.[^@ \.]{2,}$).

For this reason, I got to hand a point against regex, leaving the score tied.

Principles

It all comes down to principles - which one is a healthier method to code - with or without Regex? Which one is more efficient? Which one provides cleaner results?

Rather than brute forcing, Regex allows for the automated search or parsing of text. Sure, it may not look pleasant in your code, but it is highly efficient - so if you're not a time-waster, I would suggest using Regex.

However, if your aim is to write clean code for, say, an application, then try to employ other methods such as list comprehension to search through text, or if all else fails, brute force it on your own.

Clearly, when it comes to code, efficiency is the deciding factor, and I have to hand it over to Regex for this round, breaking the tie.

Conclusion

Regex takes the lead for efficiency and real-world uses over other brute forcing and comprehension methods, therefore it can come in real handy and you should learn it; after all, the only main downside is the fact that it can make your code unclean.

If you are struggling to learn Regex and read this article for an opinion on whether your struggles are necessary or not, here are some good resources I would recommend you use to learn Regex: