REGULAR EXPRESSION
Larry Nung
AGENDA
Syntax
Example
Reference
Q & A
2
SYNTAX
3
SPECIAL CHARACTERS
4
Metacharacter Description
* Zero or more
+ One or more
? Zero or one; Not greedy
^ Beginning of string
$ End of string
. Any
[] Group
{} Marks the start and end of a quantifier expression
() Capture
| or
 Escape
METACHARACTERS
5
Metacharacter Description
b Word boundary
B Non word boundary
d Decimal digit
Equivalent to [0-9]
D Non decimal digit
Equivalent to [^0-9]
w Word
Equivalent to [a-zA-Z_0-9]
W Non word
Equivalent to [^a-zA-Z_0-9]
[xyz] Group
[^xyz] Negated group
[a-z] Group
METACHARACTERS
6
Metacharacter Description
[^a-z] Negated group
{n} Exactly N
{n,} At least N
{n,m} From N to M
(pattern) Capture
(?<name>pattern) Named capture
(?:pattern) Non capture
(?=pattern) Positive lookahead
(?!pattern) Negative lookahead
(?<=pattern) Positive lookbehind
(?<!pattern) Negative lookbehind
cx CTRL char
xn ASCII value
NONPRINTING CHARACTERS
7
Metacharacter Description
f Form feed
Equivalent to x0ccL
n New line
Equivalent to x0acJ
r Carriage return
Equivalent to x0dcM
s Whitespace
Equivalent to [fnrtv]
S Non whitespace
Equivalent to [^fnrtv]
t Tab
Equivalent to x09cI
v Vertical tab
Equivalent to x0bcK
ORDER OF PRECEDENCE
8
Operator or operators Description
 Escape
(), (?:), (?=), [] Parentheses and brackets
*, +, ?, {n}, {n,}, {n,m} Quantifiers
^, $, anymetacharacter Anchors and sequences
| Alternation
EXAMPLE
9
EXAMPLE
 Digits
 ^[0-9]*$
 Alphabetic characters
 ^[a-zA-Z]*$
 Alpha-Numeric characters
 ^[a-zA-Z0-9]*$
 Special characters
 [W_]+ 10
EXAMPLE
 Lowercase alphabetic characters
 ^([a-z])*$
 Uppercase alphabetic characters
 ^([A-Z])*$
11
EXAMPLE
 Email
 ^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4})*$
 IP
 ^(?:(?:25[0-5]|2[0-4]d|[01]dd|d?d)(?(?=.?d).)){4}$
 Url
 ^(((http|https|ftp)://)?([[a-zA-Z0-9]-.])+(.)([[a-zA-Z0-
9]]){2,4}([[a-zA-Z0-9]/+=%&_.~?-]*))*$
12
EXAMPLE
 Date (MM/DD/YYYY)
 ^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[-
/.](19|20)?[0-9]{2})*$
 Date (YYYY/MM/DD)
 ^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-
9]|3[01]))*$
 Time
 ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]
13
EXAMPLE
 Tel
 [0-9]{2}-[0-9]{7}
 Mobile
 [0-9]{4}-[0-9]{3}-[0-9]{3}
 ID No
 ([A-Z]|[a-z])d{9}
 HTML element
 ^<([a-z]+)([^<]+)*(?:>(.*)</1>|s+/>)$ 14
EXAMPLE
 Strong password
 ^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$
 Credit card
 ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-
9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-
1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-
9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$
 MasterCard credit card
 ^(5[1-5][0-9]{14})*$
15
EXAMPLE
 Zip code
 ^([0-9]{5}(?:-[0-9]{4})?)*$
16
REFERENCE
17
REFERENCE
 正規表示式 - 維基百科,自由的百科全書
 https://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%9
9%E8%A1%A8%E8%BE%BE%E5%BC%8F
 正則表達式30分鐘入門教程
 http://deerchao.net/tutorials/regex/regex.htm
 正規化表示法 - ProgWiki
 http://www.player.idv.tw/prog/index.php/%E6%AD%A3%
E8%A6%8F%E5%8C%96%E8%A1%A8%E7%A4%BA
%E6%B3%95
18
REFERENCE
 正則表達式語法
 https://msdn.microsoft.com/zh-
cn/library/ae5bf541(v=vs.100).aspx
 Regular Expression Library
 http://www.regexlib.com/?AspxAutoDetectCookieSuppor
t=1
 Regular expression 常用驗證 | 程式設計筆記
 http://readily-notes.blogspot.tw/2010/09/regex.html
19
REFERENCE
 37 Tested PHP, Perl, and JavaScript Regular
Expressions
 http://www.virtuosimedia.com/dev/php/37-tested-php-
perl-and-javascript-regular-expressions
20
Q&A
21
QUESTION & ANSWER
22

Regular expression