Regular Expression Cheat Sheet

This morning I had to program some regular expressions. As usual, I found myself Googling for some help. I used to have a nice cheat sheet for regular expressions, but I appear to have lost it. Let's create another one.

Examples

NamePatternDescription
Integer\d+Match a sequence of digits (must have at least 1)
Real Number\d*(.\d*)?Allows numbers before and after the decimal point (cannot end with a decimal though)
Hexadecimal[0-9a-f]+Hexdecimal digits (0-9 or a-f)
Date\d{4}-\d{2}-\d{2}Date in "yyyy-mm-dd" format

Character Expressions

CharactersDescriptions
[abc]Find one character from the options between the brackets
[^abc]Find one character NOT between the brackets
[0-9]Find one character in the range 0 to 9

Character Shortcuts

ShortcutEquivalentDescription
.[^\n]Any character (except a line break)
\d[0-9]Digit (0-9)
\s[ \t\n\r\v]Whitespace (space, tab, newline, carriage return, vertical tab)
\w[a-zA-Z0-9_]"Word" character (typically: ascii letter, digit, or underscore)

Escaped Characters

Use "\" to escape the following special characters: .*+?$^/[{()}]\

Groups

GroupDescriptionExampleExample Match
\|"OR": match one of the terms22\|33\|4433
(...)Capturing group. This can be used to return a specific part of the text or to reference it laterA(nt\|pple)Apple (capturing "pple")
(?...)Non-capturing group.A(?:nt\|pple)Apple (nothing captured)
\1Contents of group 1r(\w)g\1xregex
\2Contents of group 2(\d\d)\+(\d\d)=\2\+\112+65=65+12

Repetitions

SymbolDescription
?Once or none
*Zero or more times
+One or more times
{3}Exactly 3 times
{2,4}2-4 times
{3,}3 or more times

Anchors and Boundaries

SymbolDescription
^Start of text
$End of text

References