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.
- The .NET Framework
- Apache HTTP Server
- Google Analytics
- Google Code Search
- Yahoo! Pipes
- Microsoft Exchange Server
- Windows PowerShell
- Oracle Database
- Any decent programmer's text editor or IDE
- And thousands more…
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 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
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”
Ah yes, how could I forget number 11… Regular expressions give you crime-fighting superpowers.
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!
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.
@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.
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.
You got any special regular expressions book you would recommend?
Mastering Regular Expressions, 3rd Edition
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.
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.
68DriftKing isn’t Steve, but if you see RegexL0v3r you can bet it is.
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.
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.
Regular Expressions are every where , for example
In password validation
You should really learn it by heart. It would be really helpful.
For python documentation on Regex there is an error “404 Not Found”.