22
REGULAR EXPRESSION Larry Nung

Regular expression

Embed Size (px)

Citation preview

Page 1: Regular expression

REGULAR EXPRESSIONLarry Nung

Page 2: Regular expression

AGENDASyntaxExampleReferenceQ & A

2

Page 3: Regular expression

SYNTAX3

Page 4: Regular expression

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

Page 5: Regular expression

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

Page 6: Regular expression

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

Page 7: Regular expression

NONPRINTING CHARACTERS

7

Metacharacter

Description

\f Form feedEquivalent to \x0c\cL

\n New lineEquivalent to \x0a\cJ

\r Carriage returnEquivalent to \x0d\cM

\s Whitespace Equivalent to [\f\n\r\t\v]

\S Non whitespaceEquivalent to [^\f\n\r\t\v]

\t TabEquivalent to \x09\cI

\v Vertical tabEquivalent to \x0b\cK

Page 8: Regular expression

ORDER OF PRECEDENCE

8

Operator or operators

Description

\ Escape(), (?:), (?=), [] Parentheses and brackets*, +, ?, {n}, {n,}, {n,m}

Quantifiers

^, $, \anymetacharacter

Anchors and sequences

| Alternation

Page 9: Regular expression

EXAMPLE9

Page 10: Regular expression

EXAMPLE Digits

^[0-9]*$

Alphabetic characters ^[a-zA-Z]*$

Alpha-Numeric characters ^[a-zA-Z0-9]*$

Special characters [\W_]+ 10

Page 11: Regular expression

EXAMPLE Lowercase alphabetic characters

^([a-z])*$

Uppercase alphabetic characters ^([A-Z])*$

11

Page 12: Regular expression

EXAMPLE Email

^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$

IP ^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\

d)\.)){4}$

Url ^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-

zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$12

Page 13: Regular expression

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

Page 14: Regular expression

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

Page 15: Regular expression

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

Page 16: Regular expression

EXAMPLE Zip code

^([0-9]{5}(?:-[0-9]{4})?)*$

16

Page 17: Regular expression

REFERENCE17

Page 19: Regular expression

REFERENCE 正則表達式語法

https://msdn.microsoft.com/zh-cn/library/ae5bf541(v=vs.100).aspx

Regular Expression Library http://www.regexlib.com/?

AspxAutoDetectCookieSupport=1

Regular expression 常用驗證 | 程式設計筆記 http://

readily-notes.blogspot.tw/2010/09/regex.html 19

Page 21: Regular expression

Q&A21

Page 22: Regular expression

QUESTION & ANSWER

22