Regex Cheatsheet

Regular Expressions (or regex) are, loosely speaking, a way to find patterns in text. Here’s my incomplete, not particularly useful cheatsheet.

metacharacters (special characters that mean something): .^$*+?{[]\|()

Metacharacter meanings:
. = Any character except newline
^a = Anything but a
[] = Character class (i.e. [a-z] = any lowercase letter)
a{x,y} = Find a number of 'a's in a row with a minimum of x and a max of y. E.G. a{3,6} would find "aaaa" but not "aa"
a* = Zero or more 'a's
a+ = One or more 'a's
a? = Zero or one 'a's

Escaped characters:
\d = Decimal digit (i.e. 0-9)
\D = Non-decimal character (i.e. [^0-9] anything but 0-9)
\s = whitespace character (i.e. [\t\n\r\f\v] i.e. spaces, tabs newlines etc)
\S = non-whitespace char (i.e. [^\t\n\r\f\v] )
\w = alphanumeric char (i.e. [a-zA-Z0-9_] )
\W = non-alphanumeric (i.e. [^a-zA-Z0-9_] )

e.g. [\s,.] finds whitespace char OR "," OR "."


About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s