Regular Expressions Cheat Sheet

ADVERTISEMENT

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

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go