Regular Expression 說明及範例

Metacharacters Defined

Metacharacter Examples

MChar

Definition

Pattern

Sample Matches

^

匹配字串開頭

^abc

abc, abcdefg, abc123, …

$

匹配字串結尾

abc$

abc, endsinabc, 123abc, …

.

任何字元(特殊字元除外)

a.c

abc, aac, acc, adc, aec, …

|

擇一匹配

text|ted

ted, text

{…}

量詞Explicit quantifier notation.

ab{2}c

abbc

[…]

括號內任一字元

a[bB]c

abc, aBc

(…)

Logical grouping of part of an expression.

(abc){2}

abcabc

*

0個或是N個先前的字元

ab*c

ac, abc, abbc, abbbc, …

+

1個或是N個先前的字元

ab+c

abc, abbc, abbbc, …

?

0個或是1個先前的字元

ab?c

ac, abc

\

跳脫字元或顯示符號本身

a\sc

a c


Escaped Char

Description

ordinary characters

Characters other than . $ ^ { [ ( | ) ] } * + ? \ match themselves.(顯示特殊符號)

\a

Matches a bell (alarm) \u0007.

\b

Matches a backspace \u0008 if in a []; otherwise matches a word boundary (between \w and \W characters). 比對英文字的邊界,例如空格

\t

Matches a tab \u0009.

\r

Matches a carriage return \u000D.

\v

Matches a vertical tab \u000B.

\f

Matches a form feed \u000C. 文字中有發生換頁的行為 則可以比對成功

\n

Matches a new line \u000A. 換行符號

\e

Matches an escape \u001B.

\o40

比對八進位,其中40是八進位數目

\x20

比對十六進位,其中20是十六進位數目

\cC

Matches an ASCII control character; for example \cC is control-C.

\u0020

Matches a Unicode character using a hexadecimal representation (exactly four digits).

\*

When followed by a character that is not recognized as an escaped character, matches that character. For example, \* is the same as \x2A.

/t

Tab字元

Char Class

Description

[aeiou]

括號內任一字元

[^aeiou]

括號內字元之外的字元

[0-9a-fA-F]

表示連續的數字或字母

\p{name}

Matches any character in the named character class specified by {name}. Supported names are Unicode groups and block ranges. For example, Ll, Nd, Z, IsGreek, IsBoxDrawing.

\P{name}

Matches text not included in groups and block ranges specified in {name}.

\w

等同[a-zA-Z0-9_],任何字母、數字和底線

\W

等同[^a-zA-Z0-9_]ASCII任何字元之外

\s

任一個空白字元(White space character),\s is equivalent to [ \f\n\r\t\v].

\S

任一個非空白字元,\S is equivalent to [^ \f\n\r\t\v].

\d

等同[0-9],任何數字

\D

等同[^0-9],任何數字之外的字元

(?=p)

要求後面的字元必須匹配p(回傳時不包含p字元)

(?!p)

要求後面的字元不匹配p

測試regular repress的網站

myregexp

RegExLib