Are Regular Expressions Worth It?

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.


  1. I agree that ….

    ” Sometimes spending 10 minutes getting the perfect expression is worth it. Other times it’s not.”

    My intern is always wanting to use regex for things and so we’ll both look for 10 minutes and usually I’ll find something in the language, right now I’m programming in ruby, that does the same thing as a regex would do.

    I think it makes a lot of sense to look and see if your language has something that can do this for you if you’re running the text through code first, because you can write regex incorrectly and get back some weird results. It’s also very hard to read regular expressions. Readable code is priority number one!

    Regex does have it’s place though, and I think a lot of developers, SDET’s, and manual testers should get better at it.

    1. I like your point about readable code. I completely agree – I’ve used regexes before in code and hated that it’s really difficult to understand.

      It’s worth being familiar enough to use sometimes… like in Notepad++. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *