10 Reasons to Learn and Use Regular Expressions

10. Regular expressions are everywhere

Here's a short list of programming languages and tools that support regular expressions. The links are to their regex documentation.

9. Regular expression mastery can help you stand out from the crowd

Regular expressions might be everywhere, but many experienced programmers are intimidated by them. Knowing how to use regular expressions effectively is a valuable skill that can quickly make your peers take notice.

8. Wielding regular expressions can make you feel like a mighty wizard

Regular expressions can be difficult to master, but doing so is that much more rewarding as a result. Writing a line of cryptic letters and symbols that does what might otherwise take hundreds of lines of code can feel pretty cool.

7. If your search is simple, regular expression syntax is simple

Want to match the word "cat"? The regex is simply cat. ^cat matches "cat" at the beginning of the string, cat$ matches at the end, and cat|dog matches "cat" or "dog". Most regex syntax is very simple once you get the hang of it.

6. Regular expressions are portable

That's a bold lie, yet it's usually true for people who stick to the basics or intentionally write their regexes in a portable way. The majority of regex syntax works the same in a wide variety of programming languages and tools.

5. Regular expressions can help you write short code

This can be especially helpful in JavaScript, where keeping code length down is important for people with slow Internet connections. And although regexes can be hard to read, I'd rather spend a minute stepping through the logic of a regex than doing the same thing with a page full of code. Of course, like with most things in life it's important to find a good balance.

4. Regular expressions save time

Even for newcomers who still struggle with the syntax, regular expressions are often the fastest way to get the job done.

3. Regular expressions are fast

Although typical backtracking regex engines have so-called pathological cases which can take a very long time, regexes written with performance in mind will be fast enough for your needs in almost all cases. To ensure that's true, it's a good idea to at least get a feel for the basics of regex performance optimization.

2. Regular expressions can match just about anything

In other words, regular expressions are powerful. A regular expression guru can find many appropriate uses for regexes where the untrained user might not think to look. As the authors of Programming Perl wrote, "if you take 'text' in the widest possible sense, perhaps 90% of what you do is 90% text processing."

1. Regular expressions are fun

Like any good challenge, regexes can be a lot of fun. Tools like RegexPal can help remove a lot of the guesswork, so you can concentrate on solving problems.

… Feel free to add your own reasons why you think regexes are awesome^2 or the worst idea since unicycles.

Update: This post has been translated into Portuguese (by Fábio Luciano) and Spanish (by Fernando Briano). Thanks guys!

22 thoughts on “10 Reasons to Learn and Use Regular Expressions”

  1. Pingback: Fábio Luciano
  2. Regular expressions are also popular with the ladies and may cause weight loss. Since learning regular expressions I’ve lost 3 inches off my waist and entered a fulfilling relationship. Thanks regular expressions!

  3. Sage advice.

    It’s as well to be aware of the limitations of regular expressions, though. If you have to match something recursive they can’t parse it (well, perl’s might, but traditional ones can’t); complex patterns can be hideously unreadable if untended, and the temptation to indulge in regex heroics can encourage that; there are almost as many variations of regex grammar in common usage as there are applications and languages which feature regular expressions…

    In general, any time you find yourself thinking “there must be an easier way to do this”, you’re probably right. Often, regular expressions are that easier way. Sometimes, though, they’re the signpost.

  4. @gwenhwyfaer, that is all true and well put. As for regex flavors which are able to handle recursive constructs, they are Perl, PCRE, and .NET. See tag recursion on this site for related info.

  5. Regular expressions are one of those things that separate the men from the boys.

    I’ve wowed a number of my classmates by using regular expressions to do various things for assignments, and without them, I’d’ve been stuck doing the problem in a long, drawn out, recursive, nonsensical way.

    It is too valuable a tool to be ignored.

  6. Holy craps. Thanks for the heads up, Matt. 68DriftKing lists himself as John Rikki, a 36-year-old male who works in construction. He lives a smokeless, booze-free life in Manahawkin, USA, and prefers not to say if he wants children. I tried to start up a chat with him and politely requested that he delete my photo, but alas, he denied my chat request. I then left a comment, but he deleted that within a few minutes. At least he’s a handsome young devil…

    Edit: Looks like he went ahead and removed the photo a few hours later.

  7. Pingback: Picando Código
  8. A possible number 11; because if you don’t, you’ll have to write 200 lines of buggy code to figure out if that string contains an email address. Then another 200 to find a URL.

    And WRT to number 6; regexen might not be ultra-portable between flavours, but I can port a .net regex to a perl regex faster than you can port those 200 lines of c#… So yeah, writing in regex is much more portable than the alternative.

  9. Regular Expressions are every where , for example

    In password validation
    Data extraction
    String Replacement

    etc

    You should really learn it by heart. It would be really helpful.

Leave a Reply

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