在 Python 中,正则表达式通常以原始字符串的形式存储。 原始(raw)字符串的行为类似于普通的 Python 字符串,没有对反斜杠进行特殊处理。
Regex Syntax
例如,要将字符串 hello \ world 存储在一个普通的 Python 字符串中,我们必须写:
some_string = 'hello \\ world'
print(some_string)#hello \ world
使用raw字符串可以省去对反斜杠的转义:
some_raw_string = r'hello \ world'
print(some_raw_string)#hello \ world
由于反斜杠经常出现在正则表达式中,我将在本文中为所有正则表达式使用原始字符串。
Literals
正则表达式中的原义字符与字符本身匹配。 例如,正则表达式 r"a"将匹配在“Say! I like green eggs and ham! ” 中任意的“a”。所有字母数字字符和大多数标点符号都是正则文字。
Wildcard Character通配符
除了换行符, “.”符合任何字符
show_regex_match("Call me at 382-384-3840.", r".all")
#<Call> me at 382-384-3840.#
为了只匹配".",我们必须用反斜杠对其进行转义:
show_regex_match("Call me at 382-384-3840.", r"\.")
#Call me at 382-384-3840<.>#
通过使用句点字符来标记模式中不同的部分,我们构造一个正则表达式来匹配电话号码。 例如,我们可以将原来的电话号码382-384-3840替换为。 ,将破折号保留为文本。 这将导致正则表达式…-…-…。
show_regex_match("Call me at 382-384-3840.", "...-...-....")
#all me at <382-384-3840>.#
#也会这样#
show_regex_match("My truck is not-all-blue.", "...-...-....")
#My truck is <not-all-blue>.#
Character Classes
字符类匹配指定的字符集。 若要创建字符类,将所需字符集包装在方括号[]中。
show_regex_match("I like your gray shirt.", "gr[ae]y")
##匹配gray
show_regex_match("I like your grey shirt.", "gr[ae]y")
##匹配grey
show_regex_match("I like your graey shirt.", "gr[ae]y")
##不匹配
[0-9] 代表匹配所有数字
[a-z] 代表匹配所有小写字母
[A-Z]代表匹配所有大写字母
phone_regex = r'[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
show_regex_match("Call me at 382-384-3840.", phone_regex)
##匹配382-384-3840
Negated Character Classes
否定字符类匹配除类中字符以外的任何字符。 要创建一个否定的字符类,在[ ^ ]中包装否定的字符。
show_regex_match("The car parked in the garage.", r"[^c]ar")
##匹配The car <par>ked in the <gar>age.
Quantifiers
为了创建一个匹配电话号码的正则表达式,我们可以:
[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
它匹配3个数字、一个破折号、3个以上数字、一个破折号和4个以上数字。
quantifiers允许我们匹配一个模式的多个连续出现。 我们可以用花括号{}指定重复匹配的次数。
phone_regex = r'[0-9]{3}-[0-9]{3}-[0-9]{4}'
show_regex_match("Call me at 382-384-3840.", phone_regex)
#Call me at <382-384-3840>.#
{ m, n }:匹配前面的字符 m 到 n 次。
{ m }:与前面的字符精确匹配 m 次。
{ m, }:与前面的字符至少匹配 m 次。
{ ,n }:最多匹配前面的字符 n 次。
Shorthand Quantifiers
“*” 等同于{0,}:将前面的字符匹配0次或更多次
“+” 等同于{1,}:匹配前面的字符1或更多次
"?"等同于 {0,1}:匹配前面的字符0或1次
show_regex_match('He screamed "Aaaah!" as the cart took a plunge.', "Aa*h!")
#匹配<Aaaah>#
Quantifiers are greedy
量词总是返回尽可能长的匹配,这有时会返回我们不想要的结果:
比如我们试图匹配311和911,但也匹配“and” ,因为“<311 and 911>”是最长的匹配。
show_regex_match("Remember the numbers <311> and <911>", "<.+>")
在许多情况下,使用一个更具体的字符类可以防止这些错误匹配:
show_regex_match("Remember the numbers <311> and <911>", "<[0-9]+>")
Anchoring
有时,我们想只匹配字符串的开头或结尾。 特殊字符 ^ 只会把出现在字符串的开头的词匹配;特殊字符 $只会把出现在字符串的开头的词匹配。 例如,正则表达式 well $只匹配字符串末尾的 well 。
show_regex_match('well, well, well', r"well$")
#well, well, <well>#
同时使用 ^ 和 $需要正则表达式匹配全部的字符串。
phone_regex = r"^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
show_regex_match('382-384-3840', phone_regex)
#<382-384-3840>#
Escaping Meta Characters
比如说. $这种都有特殊意义的字符,我们如果想匹配必须用\来转译
show_regex_match("Call me at [382-384-3840].", "\.")
#all me at [382-384-3840]<.>#
Shorthand Character Sets
[a-zA-Z0-9] 等同于 \w
[^a-zA-Z0-9] 等同于\W
[0-9]等同于 \d
[^0-9]等同于 \D
空格 [\t\n\f\r\p{Z}] 等同于\s
非空格[^\t\n\f\r\p{z}] 等同于\S