Regular Expression Cheat Sheet
Collected from various sources
Key: Multifarious,
java only, p3 only,
warning,
caution, shared
|
POSIX
|
perl
php
python
|
java
|
vbScript
|
.NET
|
Anchors
|
|||||
Start of string or start
of line in a multi-line pattern
|
^
|
||||
Start of string
|
\A
|
^
|
|||
Start of line
|
^
|
||||
End of string, or end of
line in a multi-line pattern
|
$
|
||||
End of string
|
\Z
|
$
|
|||
End of line
|
$
|
||||
Word
boundary
|
\b
|
||||
Not
word boundary
|
\B
|
||||
Start of Word
|
\<
|
||||
End of word
|
\>
|
||||
Character Classes
|
|||||
Control
Character
|
\c
|
||||
White
space [\n\r\f\t\v]
|
\s
|
||||
Not
White space [^\n\r\f\t\v]
|
\S
|
||||
Digit [0-9]
|
\d
|
||||
Not
digit [^0-9]
|
\D
|
||||
Word [a-zA-Z0-9_]
|
\w
|
||||
Not
word [^a-zA-Z0-9_]
|
\W
|
||||
Hexadecimal digit
|
\x
|
||||
Octal digit
|
\0
|
||||
Assertions
|
|||||
Look ahead
|
?=
|
(?=...)
|
|||
Negative look ahead
|
?!
|
(?!...)
|
|||
Look behind
|
?<=
|
(?<=...)
|
|||
Negative look behind
|
?!=
?<!
|
(?<!...)
|
|||
Only once sub expression
|
?>
|
||||
Condition [if then]
|
?()
|
||||
Condition [if then else]
|
?()|
|
||||
Comment
|
?#
|
||||
Quantifiers
|
|||||
0 or more
|
*
|
||||
1 or more
|
+
|
||||
0 or 1
|
?
|
?[1]
|
|||
Match 0 or more times, but a few times as possible
|
*?
|
||||
Match 1 or more times, but a few times as possible
|
+?
|
||||
Match 0 or 1 times, but as few times as possible
|
??
|
||||
Match at least x times, no more than y times, and as few times
as possible
|
{x,y}?
|
||||
Exactly x
|
{x}
|
||||
x or more
|
{x,}
|
||||
x,
y, z
|
{x,z}
|
||||
Escapes
|
|||||
Escape following
character
|
\
|
||||
Begin literal sequence
|
\Q
|
||||
End literal sequence
|
\E
|
||||
Special Characters
|
|||||
New
Line
|
\n
\x0A
\O059
|
||||
Carriage
Return
|
\r
|
||||
Tab
|
\t
|
||||
Vertical
tab
|
\v
|
||||
Form
feed
|
\f
|
||||
Groups and Ranges
|
|||||
Any character except new
line
|
.
|
||||
a or b
|
(a|b)
|
||||
Group
|
(…)
|
||||
Passive (non-capturing
group)
|
(?:...)
|
||||
Range
|
[abc]
|
||||
Not range
|
[^abc]
|
||||
Letter a – q
|
[a-q]
|
||||
Uppercase letter A – Q
|
[A-Q]
|
||||
Digit 0 – 7
|
[0-7]
|
||||
nth group/sub pattern
|
\n
|
||||
Pattern modifiers
|
|||||
Global match
|
g
|
||||
Case insensitive
|
i
|
||||
Multiple Lines
|
m
|
||||
Treat as single line
|
s
|
||||
Allow comments and white
space in pattern
|
x
|
||||
Evaluate replacement
|
e
|
||||
Ungreedy pattern
|
U
|
||||
String replacement
|
|||||
nth non-passive group
|
$n
|
||||
“xyz” in /^(abc(xyz))$/
|
$2
|
||||
“xyz” in /^(?:abc)(xyz)$/
|
$1
|
||||
Before matched string
|
$`
|
||||
After matched string
|
$’
|
||||
Last matched string
|
$+
|
||||
Entire matched string
|
$&
|
Character
|
Abbr
|
Hex
|
Octal
|
Decimal
|
|
Null character
|
NUL
|
00
|
000
|
0
|
|
Form feed
|
\f
|
FF
|
0C
|
014
|
12
|
Line Feed
|
\n
|
LF
|
0A
|
012
|
10
|
Carriage return
|
\r
|
CR
|
0D
|
015
|
13
|
Horizontal Tab
|
\t
|
HT
|
09
|
011
|
9
|
Vertical tab
|
\v
|
VT
|
0B
|
013
|
11
|
Carriage Return Line Feed DOS
|
\r\n
|
CRLF
|
Examples
Sample
|
RegEx
|
IP Address
|
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
|
Email address
|
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
|
URL
|
(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?
|
No comments:
Post a Comment