Regular Expressions Cheat Sheet

ADVERTISEMENT

Regular Expressions Cheat Sheet
by
Dave Child (DaveChild)
via
Anchors
Assertions
Groups and Ranges
^
Start of string, or start of line in multi-line
?=
Lookahead assertion
.
Any character except new line (\n)
pattern
?!
Negative lookahead
(a|b)
a or b
\A
Start of string
?<=
Lookbehind assertion
(...)
Group
$
End of string, or end of line in multi-line
?!= or ?<!
Negative lookbehind
(?:...)
Passive (non-c​ a pt​ u ring) group
pattern
?>
Once-only Subexp​ r ession
[abc]
Range (a or b or c)
\Z
End of string
?()
Condition [if then]
[^abc]
Not (a or b or c)
\b
Word boundary
?()|
Condition [if then else]
[a-q]
Lower case letter from a to q
\B
Not word boundary
?#
Comment
[A-Q]
Upper case letter from A to Q
\<
Start of word
[0-7]
Digit from 0 to 7
\>
End of word
Quanti​ f iers
\x
Group/​ s ub​ p attern number "​ x "
*
0 or more
{3}
Exactly 3
Ranges are inclusive.
Character Classes
+
1 or more
{3,}
3 or more
\c
Control character
Pattern Modifiers
?
0 or 1
{3,5}
3, 4 or 5
\s
White space
Add a ? to a quantifier to make it ungreedy.
g
Global match
\S
Not white space
i *
Case-i​ n se​ n sitive
\d
Digit
Escape Sequences
m *
Multiple lines
\D
Not digit
\
Escape following character
s *
Treat string as single line
\w
Word
\Q
Begin literal sequence
x *
Allow comments and whitespace in
\W
Not word
pattern
\E
End literal sequence
\x
Hexade​ c imal digit
e *
Evaluate replac​ e ment
"​ E sc​ a pi​ n g" is a way of treating characters
\O
Octal digit
which have a special meaning in regular
U *
Ungreedy pattern
expres​ s ions literally, rather than as special
* PCRE modifier
POSIX
charac​ t ers.
[:upper:]
Upper case letters
String Replac​ e ment
Common Metach​ a ra​ c ters
[:lower:]
Lower case letters
$n
nth non-pa​ s sive group
^
[
.
$
[:alpha:]
All letters
$2
"​ x yz​ " in /^(abc​ ( xy​ z ))$/
{
*
(
\
[:alnum:]
Digits and letters
$1
"​ x yz​ " in /^(?:a​ b c)​ ( xyz)$/
+
)
|
?
[:digit:]
Digits
$`
Before matched string
<
>
[:xdigit:]
Hexade​ c imal digits
$'
After matched string
The escape character is usually \
[:punct:]
Punctu​ a tion
$+
Last matched string
[:blank:]
Space and tab
$&
Entire matched string
Special Characters
[:space:]
Blank characters
Some regex implem​ e nt​ a tions use \ instead of $.
\n
New line
[:cntrl:]
Control characters
\r
Carriage return
[:graph:]
Printed characters
\t
Tab
[:print:]
Printed characters and spaces
\v
Vertical tab
[:word:]
Digits, letters and underscore
\f
Form feed
\xxx
Octal character xxx
\xhh
Hex character hh
By Dave Child (DaveChild)
Published 19th October, 2011.
Sponsored by
Last updated 12th May, 2016.
Learn to solve cryptic crosswords!
Page 1 of 1.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go