I recently reviewed a pull request with a few commits focused on regular expression code changes. It's time to re-create a quick regular expression cheat sheet.

Common expressions

Category Expression Description
Special Characters .
\n
\f
\t
\xhhhh
any character
newline character
form feed character
tab character
Unicode character defined by hexadecimal number, i.e. \xFFFF
Quantifiers +
?
*
{x,y}
1 or more
0 or one
0 or more
at least ‘x’ but no more that ‘y’ occurrences
Character Sets \s
\S
\d
\D
\w
\W
[a-x]
[^a-x]
whitespace character
non-whitespace character
digit character (0-9)
non-digit character
any letter or digit or underscore (_) character
non-word character
characters in the range of a to x, excluding yz
characters except in the range of a to x, i.e. y and z
Anchoring ^
$
\b
\B
if the first character, indicates that the match starts at beginning of string
Match must continue to end of string
word boundary
non-word boundary

An example

Simple regular expression to validate date example
  1. The first character ^ indicates that the next match (19|20) starts at beginning of string.
  2. We either need a 19 or a 20 at the beginning of the string.
  3. Next we have two digit character (0-9).
  4. Next we have a range of valid characters, in this case minus, slash, space and dot.
  5. Next we have two possible combinations of digits.
    5.1 Either we have a 0, followed by a digit in the range of 1-9, i.e. 01 to 09.
    5.2 Or we have a 1, followed by a zero, one or two, i.e. 10, 11 or 12.
  6. The last character $ indicates that the next match must continue to end of string.
  7. Next we have three possible combinations of digits.
    7.1 Either we have a 0, followed by a digit in the range of 1-9, i.e. 01 to 09.
    7.2 Or we have a 1 or a 2, followed by a digit in the range of 0-9, i.e. 10 to 29.
    7.3 Or we have a three, followed by a zero or a one, i.e. 30 - 31.

If you need more examples, go to regularexpressions.info.