Table of Contents

Regex


Characters

\d one digit from 0 to 9
\w word character: ASCII letter, digit or underscore
\s whitespace character: space, tab, newline, carriage return, vertical tab
\D One character that is not a digit
\W One character that is not a word character )
\S One character that is not a whitespace character
\metachar Escape a metacharacter “^{+<[*)>.(|$\?|”
\t Tab
\r Carriage return character
\n Line feed character
\r\n Line separator

Quantifiers

a? Zero or one of a
a* Zero or more of a
a+ One or more of a
[0-9]+ One or more of 0-9
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
a* Greedy quantifier
a*? Lazy quantifier
a*+ Possessive quantifier

Character Classes

[abc] A single character of: a, b or c
[^abc] A character except: a, b or c
[a-z] A character in the range: a-z
[^a-z] A character not in the range: a-z
[0-9] A digit in the range: 0-9
[a-zA-Z] A character in the range: a-z or A-Z
[a-zA-Z0-9] A character in the range: a-z, A-Z or 0-9
[\x41] A character at hex position 41 in the ASCII table, i.e. A

Anchors

^ Start of string
$ End of string
\A Start of string
\Z End of string
\z Absolute end of string
\b A word boundary
\B Non-word boundary
\G Start of match

Group Constructs

(…) Capturing group: Match everything enclosed
(a|b) Capturing group: Match either a or b
(?P<Y>…)Capturing group named Y
(?:…) Non-capturing group: Matches and does not remember the match
(?>…) Atomic group (non-capturing)
(?|…) Duplicate subpattern group number

Lookarounds

(?=…) Positive Lookahead. x(?=y)Matches “x” only if “x” is followed by “y”
(?!…) Negative Lookahead. x(?!y) Matches “x” only if “x” is followed not by “y”
(?<=…) Positive Lookbehind (?⇐y)x Matches “x” only if “x” is preceded by “y”
(?<!…) Negative Lookbehind (?<!y)x Matches “x” only if “x” is not preceded by “y”

Cheat sheets

Online Testers