Flagrant Badassery

A JavaScript and regular expression centric blog

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!

There Are 21 Responses So Far. »

  1. […] em fevereiro 4th, 2008 Este artigo é uma tradução feita por mim, ou seja, não oficial do post 10 Reasons to Learn and Use Regular Expressions. Achei legal e quis trazer pra vocês que lêem o […]

  2. http://xkcd.com/208/

  3. Ah yes, how could I forget number 11… Regular expressions give you crime-fighting superpowers.

  4. 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!

  5. […] 10 Reasons to Learn and Use Regular Expressions: Yes, they’ll drive you crazy every now and again, but 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.” […]

  6. 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.

  7. @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.

  8. 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.

  9. You got any special regular expressions book you would recommend?

  10. Mastering Regular Expressions, 3rd Edition

  11. Unrelated to this topic, but you might find this interesting Steve.

    I was downloading from mega uploads today and a popup for megaflirt came up… and whose picture do i see, but yours:


    Are you 68DriftKing? If not, you should probably say something.

  12. 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.

  13. 68DriftKing isn’t Steve, but if you see RegexL0v3r you can bet it is.

  14. […] 10 Reasons to Learn and Use Regular Expressions 10 Reasons to Learn and Use Regular Expressions (tags: article regex programming regexp) […]

  15. 10 razones para aprender y usar Expresiones regulares…

    Algo más para agregar a la lista interminable de “Cosas que tengo que aprender”:
    10. Las expresiones regulares están en todos lados
    Una lista de lenguajes de programación y herramientas que usan expresiones regulares. Links a su document…

  16. 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.

  17. […] This is just a few great uses. When you build your regex libraries, they never die, they span all platforms. Check out ten great reasons to learn and use regular expressions. […]

  18. Don’t forget XML-Schema

    For XML-Schema, it is not a matter of being able ‘do’ want you want in procedural code. Regex is the only way to constain a string to a pattern.

  19. […] Here are more reasons to learn regex […]

  20. Regular Expressions are every where , for example

    In password validation
    Data extraction
    String Replacement


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

  21. For python documentation on Regex there is an error “404 Not Found”.

Post a Response

If you are about to post code, please escape your HTML entities (&, >, <).