Regular Expression

Regular Expression is a powerful text search engine. Here is the quick reference.

Anchors

^     start of subject
$     end of subject
\b     word boundary
\B     not a word boundary

Quantifiers

?     0 or 1, greedy
?+     0 or 1, possessive
??     0 or 1, lazy
*     0 or more, greedy
*+     0 or more, possessive
*?     0 or more, lazy
+     1 or more, greedy
++     1 or more, possessive
+?     1 or more, lazy
{n}     exactly n
{n,m}     at least n, no more than m, greedy
{n,m}+     at least n, no more than m, possessive
{n,m}?     at least n, no more than m, lazy
{n,}     n or more, greedy
{n,}+     n or more, possessive
{n,}?     n or more, lazy

Escaped Characters

\n     newline (hex 0A)
\r     carriage return (hex 0D)
\t     tab (hex 09)
\N{U+hh..}     character with Unicode code point hh..
\xhh     character with hex code hh
\x{hh..}     character with hex code hh..

Character Types

.     any character except newline
\d     a decimal digit
\D     a character that is not a decimal digit
\h     a horizontal white space character
\H     a character that is not a horizontal white space character
\N     a character that is not a newline
\s     a white space character
\S     a character that is not a white space character
\w     a “word” character
\W     a “non-word” character

Character Classes

[…]     positive character class
[^…]     negative character class
[x-y]     range (can be used for hex characters)

Capturing

(…)     capture group
(?<name>…)     named capture group
(?:…)     non-capture group

Lookahead And Lookbehind

(?=…)     positive lookahead
(?!…)     negative lookahead
(?<=…)     positive lookbehind
(?<!…)     negative lookbehind

Back References

\n     reference by number
\k<name>     reference by name