Start of string, or start of line Quantifiers Groups and Ranges
in multi-line pattern
* 0 or more {3} . Any character except new line (\n)
\A Start of string
+ 1 or more {3,} (a|b) a or b
$ End of string, or end of line in
? 0 or 1 {3,5} (...) Group
multi-line pattern
Add a ? to a quantifier to make it (?:...) Passive (non-capturing) group
\Z End of string
ungreedy.
[abc] Range (a or b or c)
\b Word boundary
Escape Sequences
[^abc] Not (a or b or c)
\B Not word boundary
\ Escape following character
[a-q] Lower case letter from a to q
\< Start of word
\Q Begin literal sequence
[A-Q] Upper case letter from A to Q
\> End of word
\E End literal sequence
[0-7] Digit from 0 to 7
Character Classes
"Escaping" is a way of treating
\x Group/subpattern number "
\c Control character characters which have a special
meaning in regular expressions Ranges are inclusive.
\s White space
literally, rather than as special
characters. Pattern Modifiers
\S Not white space
Common Metacharacters g Global match
\d Digit
^ [ . i * Case-insensitive
\D Not digit
\w Word { * ( m * Multiple lines
+ ) | s * Treat string as single line
\W Not word
< > x * Allow comments and whitespace in pattern
\x Hexadecimal digit
The escape character is usually \ e * Evaluate replacement
\O Octal digit
Special Characters U * Ungreedy pattern
POSIX
\n New line * PCRE modifier
[:upper:] Upper case letters
\r Carriage return String Replacement
[:lower:] Lower case letters
\t Tab $n nth non-passive group
[:alpha:] All letters
\v Vertical tab $2 "xyz" in /^(abc(xyz))$/
[:alnum:] Digits and letters
\f Form feed $1 "xyz" in /^(?:abc)(xyz)$/
[:digit:] Digits
\xxx Octal character xxx $` Before matched string
[:xdigit:] Hexadecimal digits
\xhh Hex character hh $' After matched string
[:punct:] Punctuation
$+ Last matched string
[:blank:] Space and tab
$& Entire matched string
[:space:] Blank characters
Some regex implementati
[:cntrl:] Control characters
[:graph:] Printed characters
[:print:] Printed characters and spaces
[:word:] Digits, letters and underscore
Assertions
?= Lookahead assertion
?! Negative lookahead
?<= Lookbehind assertion
?!= or ?<! Negative lookbehind
?> Once-only Subexpression
?() Condition [if then]
?()| Condition [if then else]
?# Comment