Regular Expression Quick Reference

Regular Expression Quick Reference

Compiled from numerous sources across the web.

Quantifiers
* 0 or more
*? 0 or more, ungreedy
+ 1 or more
+? 1 or more, ungreedy
? 0 or 1
?? 0 or 1, ungreedy
{3} Exactly 3
{3,} 3 or more
{3,5} 3, 4 or 5
{3,5}? 3, 4 or 5, ungreedy
Ranges (inclusive)
. Until new line (\n)
(a|b) a or b
(…) Group
(?:…) Passive Group
[abc] Range (a or b or c)
[^abc] Not a or b or c
[a-q] Char between a & q
[A-Q] Uppercase between A & Q
[0-7] Digit between 0 & 7
\n nth group/subpattern

POSIX Character Classes
[:upper:] Uppercase chars
[:lower:] Lowercase chars
[:alpha:] All chars
[:alnum:] Digits, chars
[:digit:] Digits
[:xdigit:] Hex digits
[:punct:] Punctuation
[:blank:] Space, tab
[:space:] Blank chars
[:cntrl:] Control chars
[:graph:] Printed chars
[:print:] Printed chars, spaces
[:word:] Digits, chars, underscores
Character Classes
\c Control char
\s Whitespace
\S Not whitespace
\d Digit
\D Not digit
\w Word
\W Not word
\xhh Hex char hh
\Oxxx Octal char xxx
Metachars (must be escaped)
[ ] { } ( ) < >
\ | ^ $ . * + ?

String Replacement (backreferences)
$n nth non-passive group
2 xyz in /^(abc(xyz))$/
1 xyz in /^(?:abc)(xyz)$/
$` Before matched string
$’ After matched string
$+ Last matched string
$& Entire matched string
$_ Entire input string
$$ Literal “$”
Anchors
^ Start of line
\A Start of string
$ End of line
\Z End of string
\b Word boundary
\B Not word boundary
\< Start of word
\> End of word

Assertions
?= Lookahead assertion
?! Negative lookahead
?<= Lookbehind assertion
?!= or ? Negative lookbehind
?> Once-only Subexpression
?() Condition [if then]
?()| Condition [if then else]
?# Comment
Special Characters
\ Escape Character
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\f Form feed
\a Alarm
[\b] Backspace
\e Escape
\N{name} Named Character

Pattern Modifiers
g Global match
i Case-insensitive
m Multiple lines
s Treat string as single line
x Allow comments & whitespace
e Evaluate replacement
U Ungreedy pattern

Samples
\d{1,2}\/\d{1,2}\/\d{4}
Date e.g. 12/22/1974
[^\s]+(?=\.(jpg|gif|png))\.\2
filename with extension jpg, gif or png
^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$
any number from 1 to 50
#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?
hexadecimal color code
(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}
8 to 16 char string with uppercase, lower case, & one digit (for passwords)
\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}
email addresses
\<(/?[^\>]+)\>
HTML Tags
[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*
Hyperlinks