What are Regular Expressions?
For those that don’t know, regexes are text patterns used to search or find and replace text. For example, the asterisk (*) usually means to match anything.
You can use a regular expression to change lots of code or text to something else. Rather than manually replace a lot of text, you can use a regex to do it for you. Think of search and replace on steroids.
Regular Expressions Aren’t Exactly Intuitive
Perhaps it is just because I haven’t spent enough time doing them. It just seems like every time I want to use them, I end up spending 5, 10, or even 30 minutes finding the right pattern to match against.
Which may lead to the conclusion… are regular expressions worth it?
Sometimes they aren’t! If I would only spend 10 minutes manually replacing text vs. 30 minutes to learn the regex to do it, the investment may not be worth it.
That being said, those 30 minutes could be used as an investment for the next time you need to use regular expressions. Think of it as the same argument we use for good code quality. It doesn’t always pay off immediately, but it pays dividends later on.
In addition, there’s no telling if those 10 minutes might have generated a lot of errors. Manually doing the same thing over and over tends to yield small oversights. Humans aren’t perfect. (Neither are machines, but they are consistent. They do just what you tell them to!)
Perhaps you miss a comma on one line, or you forget to do the replacing on another line. However that missing replacement manifests itself depends on how you process that text. A small manual error could potentially take a long time to understand and debug depending on how that text is used.
Making Smart Decisions About Automation
I hate to say this, but sometimes automating something does not pay off with time benefits. Xkcd has a funny comic on this, showing how sometimes automation goes wrong.
This is how I feel about regular expressions. Sometimes spending 10 minutes getting the perfect expression is worth it. Other times it’s not.
It takes experience and self-discipline to determine which times it makes sense and which times it doesn’t.
Personally, I tend to lean more on using it. Worst case, I learn something new and waste some time. Best case, I save myself a lot of time.
Whatever you do, please do not spend day(s)/week(s)/month(s) manually searching and replacing text. If you read this blog, you’re smarter than that. Invest some time in coming up with the right regex (or find someone else that can). When the manual labor gets that large, it’s going to be worth it.