Tuesday, December 03, 2013

RegEx'ing - The RefCardz

DZone - Refcardz - 196: Regular Expressions

The Essential Regular Expressions Cheat Sheet

A regular expression, also known as a regex or regexp, is a way of defining a search pattern. This Refcard assumes no prior knowledge of regular expressions covers basic features of regular expressions, including normal and special characters, quantifiers, capturing and non-capturing groups, and more.


About Regular Expressions

A regular expression, also known as a regex or regexp, is a way of defining a search pattern. Think of regexes as wildcards on steroids. Using wildcards, *.txt matches any-thing followed by the string ".txt". But regular expressions let you e.g.: match specific characters, refer back to previous matches in the expression, include conditionals within the expression, and much more.
This Refcard assumes basic familiarity with program structures, but no prior knowledge of regular expressions. It would be wise to play with as many of the examples as possible, since mastering regex requires a good deal of practical experience.

There is no single standard that defines what regular expressions must and must not do, so this Refcard does not cover every regex version or 'flavor'. Parts of this Refcard will apply to some flavors (for example, PHP and Perl), while the same syntax will throw parse errors with other flavors (for example, JavaScript, which has a surprisingly poor regex engine).

Regular expressions are supported in most programming and scripting languages, including Java, .NET, Perl, Python, PHP, JavaScript, MySQL, Ruby, and XML. Languages that do not natively support regular expressions nearly always have libraries available to add support. If you prefer one regex flavor to the flavor the language natively supports, libraries are often available which will add support for your preferred flavor (albeit often using a slightly different syntax to the native expressions).


If you've been following me for any length of time you'll have heard me whine about how much I stink at regex'ing. I just don't do it near enough to feel comfortable with it, let alone even approach "good." Heck, I'm so far from good I can't see it with binoculars (on a clear day, flat plain, no wind... you get the picture). Man, I'm luckily I can even spell regex. In short, this cheat sheet was written just for me! I guess since it the holidays I can share though... :P

If I were really cool, I'd have created a regex that created that title instead of using literals... But today has been a long week and well, I guess I'll leave that as an exercise for all of you.

No comments: