You are using an outdated browser which puts all net citizens at risk. As an incentive to upgrade to a current and thus much more secure product (we recommend the free Firefox browser), you won't be able to visit this site in its cute design, but in this rather boring printer-ready version only. Thank you for considering a browser update!

Regex in a Nutshell

Reg… what?

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.

Reg… how?

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.

(Sven Schwyn)

Comments

Simon said on Wednesday, February 18, 2009:

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…

taasaa said on Friday, February 20, 2009:

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!

Thdn said on Friday, February 27, 2009:

Thanks a lot ! great work ;)

Roy said on Thursday, March 05, 2009:

Very Nice! Thanks!

Jason said on Friday, April 10, 2009:

Hi Thanks for doing this, I’m sure a lot of people will find it useful, couple things.

  • in your PDF the C example is C++ / CPP
  • Also could you add Python?
  • (?P...) named groups are supported in Python, do you know if they are a standard Preg feature?

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!

Jamey said on Sunday, April 26, 2009:

Thanks loads for producign that cheatsheet, and this post. It has been a great help =D

T said on Sunday, May 24, 2009:

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!

Ravenswood said on Monday, June 01, 2009:

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 )

Dan said on Friday, June 26, 2009:

Thx a lot for the information. It is exactly was i was looking for!

lysenshi said on Saturday, July 11, 2009:

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 ;)

Asto said on Tuesday, September 01, 2009:

Wow nice Sheet! =)
Thanks a lot

edam said on Wednesday, September 23, 2009:

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.

BennyZen said on Saturday, January 16, 2010:

Great cheatsheet.
Just found his place on a wall near my desk.
Thx a lot!

shahadat hossain said on Monday, March 22, 2010:

hi sven,
its really nice sheet and handy. thanks for share.

Dr. Brain said on Saturday, April 09, 2011:

Nice work, thanks for sharing!

L'Debil said on Saturday, July 02, 2011:

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