Quick reference for regular expression patterns - now with Common patterns and Flags/Modifiers
[abc]A single character of: a, b or cExample: [abc] matches a, b, or c
[^abc]A character except: a, b or cExample: [^abc] matches d, e, f...
[a-z]A character in the range: a-zExample: [a-z] matches lowercase letters
[^a-z]A character not in the range: a-zExample: [^a-z] matches uppercase, digits, symbols
[a-zA-Z]A character in the range: a-z or A-ZExample: [a-zA-Z] matches any letter
.Any single characterExample: a.c matches abc, a1c, a@c
a|bAlternate - match either a or bExample: cat|dog matches cat or dog
\sAny whitespace characterExample: \s+ matches spaces, tabs, newlines
\SAny non-whitespace characterExample: \S+ matches visible characters
\dAny digitExample: \d+ matches 123
\DAny non-digitExample: \D+ matches abc
\wAny word characterExample: \w+ matches hello123
\WAny non-word characterExample: \W+ matches @#$
(?:...)Non-capturing groupExample: (?:abc)+ matches abc sequences
(...)Capturing groupExample: (\d+) captures numbers
a?Zero or one of aExample: ab?c matches ac, abc
a*Zero or more of aExample: ab*c matches ac, abc, abbc
a+One or more of aExample: ab+c matches abc, abbc
a{3}Exactly 3 of aExample: \d{3} matches 123
a{3,}3 or more of aExample: \d{3,} matches 123, 1234...
a{3,6}Between 3 and 6 of aExample: \d{3,6} matches 123, 1234, 12345, 123456
^Start of stringExample: ^hello matches hello at start
$End of stringExample: world$ matches world at end
\bA word boundaryExample: \bcat\b matches cat as whole word
\BNon-word boundaryExample: \Bcat\B matches cat within word
\w \d \sword, digit, whitespaceExample: \w+ \d+ \s+ matches word123 space
\W \D \Snot word, digit, whitespaceExample: \W+ matches @#$
[a-g]character between a & gExample: [a-g] matches a, b, c, d, e, f, g
[[:alnum:]]Letters and digitsExample: [[:alnum:]] matches alphanumeric characters
[[:alpha:]]LettersExample: [[:alpha:]] matches alphabetic characters
[[:ascii:]]ASCII codes 0-127Example: [[:ascii:]] matches ASCII characters
[[:blank:]]Space or tab onlyExample: [[:blank:]] matches space and tab
[[:cntrl:]]Control charactersExample: [[:cntrl:]] matches control characters
[[:digit:]]Decimal digitsExample: [[:digit:]] matches 0-9
[[:graph:]]Visible characters (not space)Example: [[:graph:]] matches printable non-space
[[:lower:]]Lowercase lettersExample: [[:lower:]] matches a-z
[[:print:]]Visible charactersExample: [[:print:]] matches printable characters
[[:punct:]]Visible punctuation charactersExample: [[:punct:]] matches punctuation
[[:space:]]WhitespaceExample: [[:space:]] matches whitespace characters
[[:upper:]]Uppercase lettersExample: [[:upper:]] matches A-Z
[[:word:]]Word charactersExample: [[:word:]] matches word characters
[[:xdigit:]]Hexadecimal digitsExample: [[:xdigit:]] matches 0-9A-Fa-f
[[:<:]]Start of wordExample: [[:<:]] matches word start boundary
[[:>:]]End of wordExample: [[:>:]] matches word end boundary
\GStart of matchExample: \G\d+ matches digits from last match position
^Start of stringExample: ^hello matches hello at start of string
$End of stringExample: world$ matches world at end of string
\AStart of stringExample: \Ahello matches hello at very start
\ZEnd of stringExample: world\Z matches world at very end
\zAbsolute end of stringExample: end\z matches end at absolute end
\bA word boundaryExample: \bword\b matches whole word
\BNon-word boundaryExample: \Bcat\B matches cat within word
\. \* \\escaped special charactersExample: \. matches literal dot
\+reserved charactersExample: \+ matches literal plus sign
\000octal escapeExample: \000 matches null character
\xFFhexadecimal escapeExample: \xFF matches character with hex value FF
\uFFFFunicode escapeExample: \u0041 matches character A
\u{FFFF}extended unicode escapeExample: \u{1F600} matches emoji
\cIcontrol character escapeExample: \cI matches tab character
\ttabExample: \t matches tab character
\nline feedExample: \n matches newline character
\vvertical tabExample: \v matches vertical tab
\fform feedExample: \f matches form feed character
\rcarriage returnExample: \r matches carriage return
\0nullExample: \0 matches null character
(?:...)Non-capturing groupExample: (?:abc)+ matches abc sequences
(...)Capturing groupExample: (\d+) captures numbers
(?>...)Atomic group (non-capturing)Example: (?>\d+) matches without backtracking
(?|...)Duplicate/reset subpattern group numberExample: (?|(a)|(b)) resets group numbering
(?#...)Comment groupExample: (?#this is a comment)abc
(?'name'...)Named Capturing GroupExample: (?'word'\w+) captures named group
(?<name>...)Named Capturing GroupExample: (?<word>\w+) captures named group
(?P<name>...)Named Capturing GroupExample: (?P<word>\w+) Python-style named group
(?imsxUJnxx)Inline modifiersExample: (?i)abc matches case-insensitive
(?imsxUJnxx:...)Localized inline modifiersExample: (?i:abc) applies case-insensitive to abc only
(?(1)yes|no)Conditional statementExample: (a)?(?(1)b|c) matches ab or c
(?(R)yes|no)Conditional statementExample: (?(R)recursive|normal) checks recursion
(?(R#)yes|no)Recursive Conditional statementExample: (?(R#)recursive|normal) checks specific recursion
(?(R&name)yes|no)Conditional statementExample: (?(R&name)yes|no) checks named recursion
(?(?=...)yes|no)Lookahead conditionalExample: (?(?=\d)\d+|\w+) matches digits or words
(?(?<=...)yes|no)Lookbehind conditionalExample: (?(?<=@)\w+|\d+) matches based on lookbehind
(?(DEFINE)...)Pre-define patterns before using themExample: (?(DEFINE)(?<word>\w+))(?&word)
(?R)Recurse entire patternExample: \(((?>[^()]++|(?R))*)\) matches nested parentheses
(?1)Match expression defined in capture group 1Example: (\w+)\s+(?1) matches repeated words
(?+1)Match expression defined in the first relative capture groupExample: (?+1) references relative group
(?&name)Match expression defined in capture group `name`Example: (?<word>\w+)\s+(?&word) matches repeated named groups
(?P=name)Match subpattern `name`Example: (?P<word>\w+)\s+(?P=word) Python-style backreference
(?P>name)Match expression defined in the capture group "{name}"Example: (?P<word>\w+)\s+(?P>word) Python-style subroutine
\1backreference to group #1Example: (\w)\1 matches doubled letters
(?=...)Positive LookaheadExample: \d+(?=px) matches numbers before px
(?!...)Negative LookaheadExample: \d+(?!px) matches numbers not before px
(?<=...)Positive LookbehindExample: (?<=\$)\d+ matches numbers after $
(?<!...)Negative LookbehindExample: (?<!un)happy matches happy not preceded by un
(*ACCEPT)Control verbExample: (*ACCEPT) forces match to succeed
(*FAIL)Control verbExample: (*FAIL) forces match to fail
(*MARK:NAME)Control verbExample: (*MARK:label) marks position for backtracking
a?Zero or one of aExample: ab?c matches ac, abc
a*Zero or more of a (greedy)Example: ab*c matches ac, abc, abbc
a+One or more of aExample: ab+c matches abc, abbc
a{3}Exactly 3 of aExample: \d{3} matches 123
a{3,}3 or more of aExample: \d{3,} matches 123, 1234...
a{3,6}Between 3 and 6 of aExample: \d{3,6} matches 123, 1234, 12345, 123456
a*?Lazy quantifierExample: a*? matches as few a's as possible
a*+Possessive quantifierExample: a*+ matches without backtracking
ab|cdmatch ab or cdExample: cat|dog matches cat or dog
gGlobalExample: /abc/g matches all occurrences
mMultilineExample: /^abc/m matches abc at line start
iCase insensitiveExample: /abc/i matches ABC, abc, Abc
xIgnore whitespace / verboseExample: Allows comments and whitespace
sSingle lineExample: . matches newline characters
uUnicodeExample: Enables Unicode matching
XeXtraExample: Extra features (Perl)
UUngreedyExample: Makes quantifiers non-greedy by default
yStickyExample: Matches from lastIndex position
dhasIndicesExample: Generate indices for substring matches
$0Complete match contentsExample: Replace with entire matched text
$1Contents in capture group 1Example: Replace with first captured group
$$Insert a dollar signExample: Replace with literal $ character
${foo}Contents in capture group `foo`Example: Replace with named group 'foo'
\x20Hexadecimal replacement valuesExample: Replace with space character (hex 20)
\x{06fa}Hexadecimal replacement valuesExample: Replace with Unicode character U+06FA
\tInsert a tabExample: Replace with tab character
\rInsert a carriage returnExample: Replace with carriage return character
\nInsert a newlineExample: Replace with newline character
\fInsert a form-feedExample: Replace with form-feed character
\UUppercase TransformationExample: Convert following text to uppercase
\LLowercase TransformationExample: Convert following text to lowercase
\ETerminate any TransformationExample: End case transformation
${1:+foo:bar}Conditional replacementExample: Replace with 'foo' if group 1 exists, else 'bar'
\[Insert the escaped literalExample: Replace with literal [ character