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|b
Alternate - match either a or bExample: cat|dog matches cat or dog
\s
Any whitespace characterExample: \s+ matches spaces, tabs, newlines
\S
Any non-whitespace characterExample: \S+ matches visible characters
\d
Any digitExample: \d+ matches 123
\D
Any non-digitExample: \D+ matches abc
\w
Any word characterExample: \w+ matches hello123
\W
Any 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
\b
A word boundaryExample: \bcat\b matches cat as whole word
\B
Non-word boundaryExample: \Bcat\B matches cat within word
\w \d \s
word, digit, whitespaceExample: \w+ \d+ \s+ matches word123 space
\W \D \S
not 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
\G
Start 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
\A
Start of stringExample: \Ahello matches hello at very start
\Z
End of stringExample: world\Z matches world at very end
\z
Absolute end of stringExample: end\z matches end at absolute end
\b
A word boundaryExample: \bword\b matches whole word
\B
Non-word boundaryExample: \Bcat\B matches cat within word
\. \* \\
escaped special charactersExample: \. matches literal dot
\+
reserved charactersExample: \+ matches literal plus sign
\000
octal escapeExample: \000 matches null character
\xFF
hexadecimal escapeExample: \xFF matches character with hex value FF
\uFFFF
unicode escapeExample: \u0041 matches character A
\u{FFFF}
extended unicode escapeExample: \u{1F600} matches emoji
\cI
control character escapeExample: \cI matches tab character
\t
tabExample: \t matches tab character
\n
line feedExample: \n matches newline character
\v
vertical tabExample: \v matches vertical tab
\f
form feedExample: \f matches form feed character
\r
carriage returnExample: \r matches carriage return
\0
nullExample: \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
\1
backreference 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|cd
match ab or cdExample: cat|dog matches cat or dog
g
GlobalExample: /abc/g matches all occurrences
m
MultilineExample: /^abc/m matches abc at line start
i
Case insensitiveExample: /abc/i matches ABC, abc, Abc
x
Ignore whitespace / verboseExample: Allows comments and whitespace
s
Single lineExample: . matches newline characters
u
UnicodeExample: Enables Unicode matching
X
eXtraExample: Extra features (Perl)
U
UngreedyExample: Makes quantifiers non-greedy by default
y
StickyExample: Matches from lastIndex position
d
hasIndicesExample: Generate indices for substring matches
$0
Complete match contentsExample: Replace with entire matched text
$1
Contents 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'
\x20
Hexadecimal replacement valuesExample: Replace with space character (hex 20)
\x{06fa}
Hexadecimal replacement valuesExample: Replace with Unicode character U+06FA
\t
Insert a tabExample: Replace with tab character
\r
Insert a carriage returnExample: Replace with carriage return character
\n
Insert a newlineExample: Replace with newline character
\f
Insert a form-feedExample: Replace with form-feed character
\U
Uppercase TransformationExample: Convert following text to uppercase
\L
Lowercase TransformationExample: Convert following text to lowercase
\E
Terminate 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