正则表达式中最常见的符号和字符

来源:互联网 发布:值得买的电子产品知乎 编辑:程序博客网 时间:2024/05/18 15:51

Notation

Description

Example RE

Symbols



literal

Match literal string valueliteral

foo

re1|re2

Match regular expressionsre1 orre2

foo|bar

.

Matchany character (except NEWLINE)

b.b

^

Match start of string

^Dear

$

Match end of string

/bin/*sh$

*

Match 0 or more occurrences of preceding RE

[A-Za-z0-9]*

+

Match 1 or more occurrences of preceding RE

[a-z]+\.com

?

Match 0 or 1 occurrence(s) of preceding RE

goo?

{N}

Match N occurrences of preceding RE

[0-9]{3}

{M,N}

Match from M to N occurrences of preceding RE

[0-9]{5,9}

[...]

Match any single character fromcharacter class

[aeiou]

[..x-y..]

Match any single character in therange fromx toy

[0-9],[A-Za-z]

[^...]

Do not match any character from character class, including any ranges, if present

[^aeiou], [^A-Za-z0-9_]

(*|+|?| {})?

Apply "non-greedy" versions of above occurrence/repetition symbols (*,+,?, {})

.*?[a-z]

(...)

Match enclosed RE and save assubgroup

([0-9]{3})?, f(oo|u)bar

Special Characters



\d

Match any decimal digit, same as [0-9](\D is inverse of \d: do not match any numeric digit)

data\d+.txt

\w

Match any alphanumeric character, same as [A-Za-z0-9_] (\W is inverse of\w)

[A-Za-z_]\w+

\s

Match any whitespace character, same as [ \n\t\r\v\f] (\S is inverse of\s)

of\sthe

\b

Match anyword boundary (\B is inverse of \b)

\bThe\b

\nn

Match saved subgroup nn (see (...) above)

price: \16

\c

Match any special character c verbatim (i.e., with out its special meaning, literal)

\., \\, \*

\A (\Z)

Matchstart (end) of string (also see^ and $ above)

\ADear


REF:Core Python Programming

0 0
原创粉丝点击