Regular expressions (or regex for short) are the Swiss army knife of string searching and manipulation, a way to prevent cascades of conditions and loops. However, some folks still consider them hard to learn and thus keep their hands off. Big mistake!
The basic idea of regex date back to the 1950s when mathematician Stephen Cole Kleene invented a notation he called regular sets. They have seen much improvement ever since and nowadays the most commonly used implementation standard is “Perl Compatible Regular Expressions” or PCRE for short. Read the full story on Wikipedia.
Here are a few helpful resources for learning regex:
And finally our handy and colorful cheat sheet:
Please keep in mind that although many programming languages and text editors implement regex nowadays, the actual implementation may differ. Expressions using niftier features are generally more likely not to work in a different environment.
And make sure you have fully understood the concept of “greediness” because if you haven’t, you’ll end up with clumsy and error prone expressions.
Feel free to submit additions and corrections as comments.
Comments
I liked your cheat sheet. It’s very handy for someone like me – impossible remember all the shortcuts and flags…
gonna link this page on my blog…
nice cheatsheet, thanks.
in grouping section – (…)(…)etc first group is \1 and $1, second group is \1 and $2 etc
in the second group it should read ”\2 and $2”
Re: Fixed that, thanks!
Thanks a lot ! great work ;)
Very Nice! Thanks!
Hi Thanks for doing this, I’m sure a lot of people will find it useful, couple things.
Re: I’ve included Python and named groups, a very useful addition by the Python herd. They have been adopted in PCRE 4.0 – although with a slightly different syntax. Thanks a lot for your input!
Thanks loads for producign that cheatsheet, and this post. It has been a great help =D
Sven,
Thank you very much for the Ruby and Java online links. That was really helpful in quickly and visually seeing where I was making a mistake.
Nice post!
This chart is great! I’ve been looking for a chart like this for years!
One minor correction: The /g (global replace) flag is not needed in PHP. PHP always acts as if there is a /g flag, and adding a /g to the end of your expression results in an error. Instead use the “limit” parameter. (See the PHP page on preg_replace for details: http://us.php.net/manual/en/function.preg-replace.php )
Thx a lot for the information. It is exactly was i was looking for!
Thanks a lot for the colorful cheat sheet, it is really great.
One thing I think you should add is the php function preg_match_all. I was struggling with preg_match with g flag to get all matches but g flag wasn’t supported, and I found that could be accomplished using preg_match_all.
Keep up the good work ;)
Wow nice Sheet! =)
Thanks a lot
What about C++ with boost’s regex library? it’s surely a much more obvious (commonly used) library that the one given?
boost::regex_search( string, boost::regex( pattern, flags ) );
boost::regex_replace( string, boost::regex( pattern, flags ), replace );
Re: Done that, thanks for the hint.
Great cheatsheet.
Just found his place on a wall near my desk.
Thx a lot!
hi sven,
its really nice sheet and handy. thanks for share.
Nice work, thanks for sharing!
hello! thank you, cheatsheet helped me a lot to move really fast from regular expressions to pcre
(We are remaking our web presence and therefore comments are temporary disabled.)