当前位置:   article > 正文

Python中的正则表达式

re.search

简介

  • 正则表达式 是包含文本和特殊字符的字符串, 为高级的文本模式匹配, 抽取, 与文本形式的搜索和替换功能提供了基础
  • Python通过标准库re模块来支持正则表达式
  • 模式匹配的两种方法完成匹配(模式匹配)

    • 搜索(search())
    • 匹配(match())

特殊符号和字符

元字符指正则表达式中的特殊符号和字符。

符号

符号描述示例
literal匹配文本字符串的字面值literal foo
`re1\re2`匹配正则表达式re1re2 `foo\bar`
.匹配任何字符(除\n之外)f.o
^匹配字符串起始部分^foo
$匹配字符串终止部分bar$
*匹配0次或者多次前面出现的正则表达式[A-Za-z0-9]*
+匹配1次或者多次前面出现的正则表达式[a-z]+\\.com
?匹配0次或者1次前面出现的正则表达式goo?
{N}匹配N次前面出现的正则表达式[0-9]{3}
{M,N}匹配M~N次前面出现的正则表达式[0-9]{5,9}
[…]匹配来自字符集的任意单一字符[aeiou]
[x-y]匹配x~y范围中的任意单一字符[0-9], [A-Za-z]
[^…]不匹配此字符集中出现的任何一个字符, 包括某一范围的字符[^aeiou], \[^A-Za-z]
`(*+?{})?`用于匹配上面频繁出现/重复出现符号的非贪婪版本(*、+、?、{}) .*?[a-z]
(…)匹配封闭的正则表达式,然后另存为子组`([0-9]{3})?,f(oou)bar`

特殊符号

符号描述示例
\d匹配任何十进制数字,与[0-9]一致data\d+.txt
\D\d相反
\w匹配任何字母数字字符,与[A-Za-z0-9]相同
\W\w相反
\s匹配任何空格字符,与[\n\t\r\v\f]相同
\S\s相反
\N匹配已保存的子组 Nprice:\1
\c逐字匹配任何特殊字符c \.,\\,\*
\A匹配字符串起始,与^相同
\Z匹配字符串结束,与$相同

扩展符号

符号描述示例
(?iLmsux) 在正则表达式本身中嵌入一个或多个特殊特殊标记 (vs. via function/method)(?x),(?im)
(?:...)匹配一个不用保存的分组(?:\w+\.)
(?P<name>...) 使用名字表示的正则分组(?P<data>)
(?#...)表示注释,所有内容会被忽略(?#comment)
(?=...)匹配条件是如果...出现在之后的位置,而不使用输入字符串;称作正向前视断言(positive lookahead assertion)(?=.com)
(?!...)匹配条件是如果...不出现在之后的位置,而不使用输入字符串;称作负向前视断言(negative lookahead assertion)(?!.net)
(?<=...)匹配条件是如果...出现在之前的位置,而不使用输入字符串;称作正向后视断言(positive lookbehind assertion)(?<=800-)
(?<!...)匹配条件是如果...不出现在之前的位置,而不使用输入字符串;称作负向后视断言(negative lookbehind assertion)(?<!192\.168\.)
`(?(id/name)YN)`Conditional match of regex Y if group with given id or name exists else N; \N is optional`(?(1)yx)`

使用管道符匹配多个正则表达式

管道符号在正则表达式中又称为择一匹配符,表示 从多个模式中选择其一 的操作。

正则表达式匹配的字符串
`athome`at,home
`r2d2c3po`r2d2,c3po
`batbetbit`bat,bet,bit

匹配任意单个字符

句点(.)符号匹配除了换行符\n以外的任何字符。无论字母、数字、空格(不包括\n换行符)、可打印字符、不可打印字符,使用.都可以匹配。

正则表达式匹配的字符串
f.0匹配在字母fo之间的任意一个字符,如:fao,f9o,f#o
..任意两个字符
.end匹配在字符串end之前的任意一个字符

注意

要显式匹配一个句点符号本身,必须使用反斜线转义句点符号的功能,例如\.

匹配起始或结尾

有些符号和相关的特殊字符用于在字符串的起始或结尾部分指定用于搜索的模式。

符号位置
^\A 起始
$\Z 结尾

简单示例如下。

正则表达式匹配的字符串
^Froms任何以From作为开头的字符串
/bin/bash$任何以/bin/bash结尾的字符串
^Subject:hi$匹配Subject:hi

匹配单词边界

符号说明
\b匹配一个单词的边界
\B匹配不是一个单词的边界

简单示例如下。

正则表达式匹配的字符串
the任何包含the的字符串
\bthe任何以the开始的字符串
\bthe\b仅仅匹配单词the
\Bthe任何包含但并不以the作为起始的字符串

创建字符集

使用[]创建字符集,可以匹配某些特定字符。

正则表达式匹配的字符串
b[aeiu]tbat,bet,bit,but
[cr][23]c2,c3,r2,r3

限定范围和否定

除了单字符外,字符集还支持匹配指定的字符范围。两个字符中间用连字符-连接,用于指定一个字符的范围。如果^紧跟在左括号后面,这个符号就表示不匹配给定字符集中任何一个字符。

正则表达式匹配的字符串
z.[0-9] z 后面跟着任何一个字符,然后跟着一个数字
[r-u][env-y][us]等价于[rstu][envwxy][us] ,比如匹配res
[^aeiou]匹配一个非元音字符
[^\t\n]不匹配制表符或换行符

使用闭包操作符实现存在性和频数匹配

符号说明
*匹配左侧的正则表达式出现零次或多次的情形,这称作Kleene闭包
+匹配一次或多次出现的正则表达式,这称作正闭包操作符
?操作符将匹配零次或者一次出现的正则表达式
{}里面或者是单值,或者是一对由逗号分隔的数值;{N}表示匹配N次;{N,M}表示匹配N~M

如果问号紧跟在任何使用闭包操作符的匹配后面,它将直接要求正则表达式引擎匹配尽可能少的次数。当模式匹配使用分组操作符时,正则表达式引擎将试图吸收匹配该模式的尽可能多的字符,这通常叫做贪婪匹配。问号要求正则表达式引擎在当前正则表达式中尽可能少地匹配字符。

简单示例。

正则表达式匹配的字符串
[dn]otdo,dot,no,not
0?[1-9]可能存在前置0的数字
[0-9]{15,16}匹配15或者16个数字
</?[^>]+>匹配HTML标签

表示字符集的特殊字符

有一些特殊字符能够表示字符集。

符号描述
\d匹配任何十进制数字,与[0-9]一致data\d+.txt
\D\d相反
\w匹配任何字母数字字符,与[A-Za-z0-9]相同
\W\w相反
\s匹配任何空格字符,与[\n\t\r\v\f]相同
\S\s相反

简单示例。

正则表达式匹配的字符串
\w+-\d+一个由字母数字组成的字符串和一串由一个连字符分割的数字

使用圆括号指定分组

有时候不仅想要知道整个字符串是否匹配我们的标准,而且想要知道能否提取任何已经成功匹配的特定字符串或者子字符串,可以使用分组来实现。

一对圆括号可以实现一下任意一个功能:

  • 对正则表达式进行分组
  • 匹配子组

对正则表达式分组,可以在整个正则表达式中使用重复的操作符。

使用圆括号进行分组的一个副作用就是,匹配模式的子字符串可以保存起来供后续使用。这些子组能够被同一次的匹配或者搜索重复调用,或者提取出来用于后续处理。

匹配子组的重要性在于,很多时候除了进行匹配操作以外,我们还想要提取所匹配的模式。

简单示例如下。

正则表达式匹配的字符串
\d+(\.\d*)?表示简单浮点数的字符串

扩展表示法

正则表达式的扩展表示法,以问号开始(?...)。他们通常用于在判断匹配之前提供标记,实现一个前视或者后视匹配,或者条件检查。

尽管这些符号使用圆括号,但是只有(?P<name>) 表示一个分组匹配,其他的都没有创建一个分组。

正则表达式匹配的字符串
(?:\w+\.)*以句点作为结尾的字符串,例如 google.,但是这些匹配不会保存下来供后续使用和数据检索
(?#comment)注释
(?=.com)如果一个字符串和后面跟着 .com 才做匹配操作,并不使用任何目标字符串
(?!.net)如果一个字符串后面不是跟着.net才做匹配操作
(?<=800-)如果字符串之前为800-才做匹配
(?<!192\.168\.)如果一个字符串之前不是192.168.才做匹配操作
`(?(1)yx)`如果一个匹配组1(\1)存在,就与y匹配;否则与x匹配

Python中的正则表达式

在Python中,re模块支持更强大而且更通用的Perl风格的正则表达式,该模块允许多个线程共享同一个已编译的正则表达式对象,也支持命名子组。

re模块

re模块函数

函数描述
compile(pattern, flags=0)使用任何可选的标记来编译正则表达式的模式,然后返回一个正则表达式对象

re模块函数和正则表达式对象的方法

函数描述返回值
match(pattern, string, flags=0)使用带有可选标记的正则表达式模式匹配字符串匹配成功,返回匹配对象;如果失败,返回None
search(pattern, string, flags=0)使用可选标记搜索字符串中第一次出现的正则表达式模式匹配成功,返回匹配对象;如果失败,返回None
findall(pattern, string[, flags])查找字符串中所有(非重复)出现的正则表达式模式匹配列表
finditer(pattern, string[, flags])findall相同,但返回的不是列表一个迭代器
split(pattern, string,max=0 )根据正则表达式的模式分隔符,split函数将字符串分割为列表,然后返回成功匹配的列表,分割最多操作max次,默认分割所有匹配成功的位置分割后的列表
sub(pattern, repl, string, count=0)使用repl替换count次正则表达式的模式在字符串中出现的位置;默认替换所有替换操作数目
purge()清除隐式编译的正则表达式模式;清除缓存

常用的匹配对象方法

函数描述
group(num=0)返回整个匹配对象;或者编号为num的特定子组
groups(default=None)返回一个包含所有匹配子组的元组(如果没有成功匹配,则返回一个空元组)
groupdict(default=None)返回一个包含所有匹配的命名子组的字典,所有的子组名称作为字典的键(如果没有匹配成功返回一个空元组)

常用的模块属性

属性描述
re.I,re.IGNORECASE不区分大小写的匹配
re.L,re.LOCALE根据所使用的本地语言环境通过\w,\W,\b,\B,\s,\S实现匹配
re.M,re.MULTILINE ^$分别匹配目标字符串中行的起始和结尾,而不是严格匹配整个字符串本身的起始和结尾
re.S,re.DOTALL . 通常匹配除了\n 之外的所有单个字符,该标记可以使.匹配换行符
re.X, re.VERBOSE通过反斜线转义,否则所有空格加上#都被忽略

使用compile函数编译正则表达式

  • 在Python中可以通过两种途径使用正则表达式:

    • re模块函数
    • 调用编译后的正则表达式对象方法
  • 在Python中由两类对象和正则表达式有关:

    • re.compile生成的表达式对象
    • 匹配对象(成功调用 match()search() 之后返回的对象)
  • 几乎所有的re模块函数都可以作为regex对象的方法。
  • 可以通过按位或操作符(|)合并使用多个标记
  • 可以使用(?FLAG)将标记嵌入到正则表达式

    1. >>> re.match(r'(?i)the', 'the')
    2. <_sre.SRE_Match object; span=(0, 3), match='the'>
    3. >>> re.match(r'(?i)the', 'The')
    4. <_sre.SRE_Match object; span=(0, 3), match='The'>
    5. >>> re.match(r'the', 'The')
    6. >>>

匹配对象以及group()groups()方法

匹配对象是成功调用match()或者search()返回的对象。匹配对象有两个主要的方法: group()groups()

group()要么返回整个匹配对象,要么根据要求返回特定子组。groups()则仅返回一个包含唯一或者全部子组的元组。如果没有子组的要求,那么当group()仍返回整个匹配时,groups()返回一个空元组。

Python正则表达式允许命名匹配。

使用match方法匹配字符串

match方法试图从字符串的起始部分对模式进行匹配。

如果匹配成功,就返回一个匹配对象;如果匹配对象失败,就返回None。

匹配对象的group()方法能够用于显示那个成功的匹配。

  1. Python 3.5.4rc1 (default, Jul 25 2017, 08:53:34)
  2. [GCC 6.4.0 20170704] on linux
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import re
  5. >>> m = re.match('foo', 'foo')
  6. >>> if m is not None:
  7. ... m.group()
  8. ...
  9. 'foo'
  10. >>> m
  11. <_sre.SRE_Match object; span=(0, 3), match='foo'>

只要模式从字符串的起始部分匹配,即使字符串比模式长,匹配也仍然能够成功。匹配结果是从较长字符串中抽取的匹配部分

  1. >>> re.match('foo', 'food on the table').group()
  2. 'foo'

使用search在字符串中查找模式

search函数在任意位置对给定正则表达式模式搜索第一次出现的匹配情况。如果搜索到成功的匹配,就会返回一个匹配对象;否则,返回None。

match相比,match只能从起始开始匹配,search可以匹配任意位置。

  1. >>> m = re.match('foo', 'seafood')
  2. >>> m.group() if m is not None else print(m)
  3. None
  4. >>> m = re.search('foo', 'seafood')
  5. >>> m.group() if m is not None else print(m)
  6. 'foo'

上面代码,使用match匹配失败,使用search则匹配成功。

匹配多个字符串

管道符号或择一匹配符号(|)的使用。

  1. >>> bt = 'bat|bet|bit'
  2. >>> m = re.match(bt, 'bat')
  3. >>> m.group() if m is not None else print(m)
  4. 'bat'
  5. >>> m = re.match(bt, 'blt') # 不能匹配
  6. >>> m.group() if m is not None else print(m)
  7. None
  8. >>> m = re.match(bt, 'He bit me') # 不能匹配
  9. >>> m.group() if m is not None else print(m)
  10. None
  11. >>> m = re.search(bt, 'He bit me') # 可以搜索到
  12. >>> m.group() if m is not None else print(m)
  13. 'bit'

匹配任何单个字符

点号.不能匹配换行符和空字符串

  1. >>> dote = '.end'
  2. >>> m = re.match(dote, 'bend') # OK
  3. >>> m.group() if m is not None else print(m)
  4. 'bend'
  5. >>> m = re.match(dote, 'end') # 不能匹配空内容
  6. >>> m.group() if m is not None else print(m)
  7. None
  8. >>> m = re.match(dote, '\nend') # 不能匹配换行符
  9. >>> m.group() if m is not None else print(m)
  10. None
  11. >>> m = re.search(dote, 'The end.') # 搜索匹配
  12. >>> m.group() if m is not None else print(m)
  13. ' end'

使用转义符,匹配点号。

  1. >>> pi = '3.14'
  2. >>> pit = '3\.14'
  3. >>> m = re.match(pit, '3.14') # 精确匹配
  4. >>> m.group() if m is not None else print(m)
  5. '3.14'
  6. >>> m = re.match(pi, '3014') # 点号匹配0
  7. >>> m.group() if m is not None else print(m)
  8. '3014'
  9. >>> m = re.match(pi, '3.14') # 点号匹配.
  10. >>> m.group() if m is not None else print(m)
  11. '3.14'

创建字符集

  1. >>> selc = '[cr][23][dp][o2]'
  2. >>> m = re.match(selc, 'c3po')
  3. >>> m.group() if m is not None else print(m)
  4. 'c3po'
  5. >>> m = re.match(selc, 'c3p1')
  6. >>> m.group() if m is not None else print(m)
  7. None

重复、特殊字符以及分组

正则表达式中最常见的情况包括:

  • 特殊字符的使用
  • 正则表达式模式的重复出现
  • 使用圆括号对匹配模式的各部分进行分组和提取操作

特殊字符与重复出现

匹配0~1个中间子域名

  1. >>> import re
  2. >>> patt = '\w+@(\w+\.)?\w+\.com'
  3. >>> m = re.match(patt, 'nobody@xxx.com') # 匹配0个中间子域名
  4. >>> m.group() if m is not None else print(m)
  5. 'nobody@xxx.com'
  6. >>> m = re.match(patt, 'nobody@xxx.yyy.com') # 匹配1个中间子域名
  7. >>> m.group() if m is not None else print(m)
  8. 'nobody@xxx.yyy.com'
  9. >>> m = re.match(patt, 'nobody@xxx.yyy.zzz.com') # 不能匹配2个中间子域名
  10. >>> m.group() if m is not None else print(m)
  11. None

匹配任意多个子域名

  1. >>> patt = '\w+@(\w+\.)*\w+\.com' # 将 ? 替换为 *
  2. >>> m = re.match(patt, 'nobody@xxx.yyy.zzz.com') # 匹配2个中间子域名
  3. >>> m.group() if m is not None else print(m)
  4. 'nobody@xxx.yyy.zzz.com'

分组

使用圆括号来匹配和保存子组,以便于后续处理。

使用group()groups()方法获取分组,其两者区别:

  • group()

    • 访问每个独立的子组
    • 获取完整匹配(不传递参数)
  • groups()

    • 获取一个包含所有匹配子组的元组
  1. >>> m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
  2. >>> m.group() # 完整匹配
  3. 'abc-123'
  4. >>> m.group(1) # 子组1
  5. 'abc'
  6. >>> m.group(2) # 子组2
  7. '123'
  8. >>> m.groups() # 全部子组
  9. ('abc', '123')

一个完整示例

  1. >>> m = re.match('ab', 'ab') # 没有分组
  2. >>> m.group() # 完整匹配
  3. 'ab'
  4. >>> m = re.match('ab', 'ab') # 没有分组
  5. >>> m.group() # 完整匹配
  6. 'ab'
  7. >>> m.groups() # 所有子组
  8. ()
  9. >>>
  10. >>> m = re.match('(ab)', 'ab') # 一个子组
  11. >>> m.group() # 完整匹配
  12. 'ab'
  13. >>> m.group(1) # 子组1
  14. 'ab'
  15. >>> m.groups() # 全部子组
  16. ('ab',)
  17. >>>
  18. >>> m = re.match('(a)(b)', 'ab') # 两个子组
  19. >>> m.group()
  20. 'ab'
  21. >>> m.group(1) # 子组1
  22. 'a'
  23. >>> m.group(2) # 子组2
  24. 'b'
  25. >>> m.groups() # 全部子组
  26. ('a', 'b')
  27. >>>
  28. >>> m = re.match('(a(b))', 'ab') # 两个嵌套子组
  29. >>> m.group() # 完整匹配
  30. 'ab'
  31. >>> m.group(1) # 子组1
  32. 'ab'
  33. >>> m.group(2) # 子组2
  34. 'b'
  35. >>> m.groups() # 全部子组
  36. ('ab', 'b')

匹配字符串的起始和结尾以及单词边界

  1. >>> m = re.search('^The', 'The end.') # 匹配
  2. >>> m.group() if m is not None else print(m)
  3. 'The'
  4. >>> m = re.search('^The', 'end. The') # 不做为开始
  5. >>> m.group() if m is not None else print(m)
  6. None
  7. >>> m = re.search(r'\bthe', 'bite the dog') # 匹配左侧边界
  8. >>> m.group() if m is not None else print(m)
  9. 'the'
  10. >>> m = re.search(r'\bthe', 'bitethe dog') # 匹配左侧边界
  11. >>> m.group() if m is not None else print(m)
  12. None
  13. >>> m = re.search(r'\Bthe', 'bitethe dog') # 匹配左侧没有边界
  14. >>> m.group() if m is not None else print(m)
  15. 'the'
  16. >>> m = re.search(r'\Bthe\B', 'bitethe dog') # 匹配两侧没有边界
  17. >>> m.group() if m is not None else print(m)
  18. None
  19. >>> m = re.search(r'\Bthe\b', 'bitethe dog') # 匹配左侧没有边界,右侧有边界
  20. >>> m.group() if m is not None else print(m)
  21. 'the'

使用findall和finditer查找每一次出现的位置

findall()

  • 查询字符串中某个正则表达式模式全部的非重复出现情况
  • match()search()的区别是,findall()总是返回一个列表

finditer()findall()类似,不过返回结果是一个迭代器。

  1. >>> re.findall('car', 'car')
  2. ['car']
  3. >>> re.findall('car', 'carry')
  4. ['car']
  5. >>> re.findall('car', 'carry the barcardi to the car')
  6. ['car', 'car', 'car']

结合分组使用

  1. >>> s = 'This and that.'
  2. >>> re.findall(r'(th\w+) and (th\w+)', s, re.I)
  3. [('This', 'that')]
  4. >>> list(re.finditer(r'(th\w+) and (th\w+)', s, re.I))[0].groups()
  5. ('This', 'that')
  6. >>> list(re.finditer(r'(th\w+) and (th\w+)', s, re.I))[0].group(1)
  7. 'This'
  8. >>> list(re.finditer(r'(th\w+) and (th\w+)', s, re.I))[0].group(2)
  9. 'that'

单个分组的多重匹配

  • 如果模式中只有一个分组,则匹配结果作为结果集合的单个元素
  • 如果模式中由多个分组,则匹配结果为元组,作为结果集的单个元素
  1. >>> s = 'this and that'
  2. >>> re.findall(r'(th\w+) and (th\w+)', s, re.I)
  3. [('this', 'that')]
  4. >>> re.findall(r'(th\w+)', s, re.I)
  5. ['this', 'that']
  6. >>>
  7. >>>
  8. >>> s = 'This and that. What, where, when, and who'
  9. >>> re.findall(r'th\w+|wh\w+', s, re.I)
  10. ['This', 'that', 'What', 'where', 'when', 'who']
  11. >>> re.findall(r'(th\w+)|(wh\w+)', s, re.I)
  12. [('This', ''), ('that', ''), ('', 'What'), ('', 'where'), ('', 'when'), ('', 'who')]
  13. >>> re.findall(r'(wh\w+)', s, re.I)
  14. ['What', 'where', 'when', 'who']
  15. >>>
  16. >>>
  17. >>> s = 'This where. That when. There who.'
  18. >>> re.findall(r'(th\w+)\s(wh\w+)', s, re.I)
  19. [('This', 'where'), ('That', 'when'), ('There', 'who')]

使用sub和subn搜索和替换

有两个函数用于实现搜索和替换功能: sub()subn()sub()返回一个替换后的字符串;subn()还返回一个表示替换的总数,替换后的字符串和替换总数作为元组返回。

  1. >>> re.sub('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
  2. 'attn: Mr. Smith\n\nDear Mr. Smith,\n'
  3. >>> re.subn('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
  4. ('attn: Mr. Smith\n\nDear Mr. Smith,\n', 2)
  5. >>> re.sub('[ae]', 'X', 'abcdef')
  6. 'XbcdXf'
  7. >>> re.subn('[ae]', 'X', 'abcdef')
  8. ('XbcdXf', 2)

使用匹配对象的group()方法除了能够取出匹配分组编号外,还可以使用\N,其中N是在替换字符串中使用的分组编号。

  1. >>> re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})', r'\2/\1/\3', '2/20/1992')
  2. '20/2/1992'
  3. >>> re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})', r'\2/\1/\3', '2/20/92')
  4. '20/2/92'

在限定模式上使用split分隔字符串

split 基于正则表达式的模式分隔字符串。可以通过为max参数设定一个值(非零)来指定最大分割数。

  1. >>> import re
  2. >>> DATA = ()
  3. >>> DATA = (
  4. ... 'Mountain View, CA 94040',
  5. ... 'Sunnyvale, CA',
  6. ... 'Los Altos, 94023',
  7. ... 'Cupertino 95014',
  8. ... 'Palo Alto CA'
  9. ... )
  10. >>> for datum in DATA:
  11. ... print(re.split(', |(?= (?:\d{5}|[A-Z]{2})) ', datum))
  12. ...
  13. ['Mountain View', 'CA', '94040']
  14. ['Sunnyvale', 'CA']
  15. ['Los Altos', '94023']
  16. ['Cupertino', '95014']
  17. ['Palo Alto', 'CA']

扩展符号

通过使用(?iLmsux)系列选项,用户可以直接在正则表达式里面指定一个或者多个标记。

re.I/re.IGNORECASE, re.M/MULTILINE

  1. >>> re.findall(r'(?i)yes', 'yes? Yes. YES!~') # 忽略大小写
  2. ['yes', 'Yes', 'YES']
  3. >>> re.findall(r'(?i)th\w+', 'The quickest way is through this tunnel.') # 忽略大小写
  4. ['The', 'through', 'this']
  5. >>> re.findall(r'(?im)(^th[\w ]+)', """ # 忽略大小写;多行
  6. ... This line is the first,
  7. ... another line,
  8. ... that line, it's the best.
  9. ... """)
  10. ['This line is the first', 'that line']

re.S/re.DOTALL

使用re.S,re.DOTALL标记,使得点号.能够用来表示换行符。

  1. >>> re.findall(r'th.+', '''
  2. ... The first line
  3. ... the second line
  4. ... the third line
  5. ... ''')
  6. ['the second line', 'the third line']
  7. >>> re.findall(r'(?s)th.+', '''
  8. ... The first line
  9. ... the second line
  10. ... the third line
  11. ... ''')
  12. ['the second line\nthe third line\n']

re.X/re.VERBOSE

re.X/re.VERBOSE标记允许用户通过抑制在正则表达式中使用空白符(除了在字符类中或者在反斜线转义中)来创建更易读的正则表达式。

  1. >>> re.search(r'''(?x)
  2. ... \((\d{3})\) # 匹配区号
  3. ... [ ] # 匹配空格
  4. ... (\d{3}) # 匹配前缀
  5. ... - # 横线
  6. ... (\d{4}) # 终点数字
  7. ... ''', '(800) 555-1212').groups()
  8. ('800', '555', '1212')

(?:...)

通过使用(?:...)符号,可以对部分正则表达式进行分组,但是并不会保存该分组用于后续的检索或应用。

  1. >>> re.findall(r'http://(?:\w+\.)*(\w+\.com)',
  2. ... 'http://goole.com http://www.google.com http://code.google.com')
  3. ['goole.com', 'google.com', 'google.com']
  4. >>> re.search(r'\((?P<areacode>\d{3})\) (?P<prefix>\d{3})-(?:\d{4})',
  5. ... '(800) 555-1212').groupdict()
  6. {'areacode': '800', 'prefix': '555'}

结合\g,使用(?P<name>)(?P=name)符号。

  1. >>> re.sub(r'\((?P<areacode>\d{3})\) (?P<prefix>\d{3})-(?:\d{4})',
  2. ... '(\g<areacode>) \g<prefix>-xxxx', '(800) 555-1212')
  3. '(800) 555-xxxx'

检索元组

  • 对于没有命名的元组,使用\N,其中N为数字
  • 对于命名的元组,可以使用\g<name>,也可以使用\N
  1. >>> m = re.search(r'(\d{4}) (?P<prefix>\d{4}) (\d{3})', '0530 8023 123')
  2. >>> m.group()
  3. '0530 8023 123'
  4. >>> m.group(1)
  5. '0530'
  6. >>> m.group(2)
  7. '8023'
  8. >>> m.group(3)
  9. '123'
  10. >>> m.groupdict()
  11. {'prefix': '8023'}
  12. >>>
  13. >>> re.sub(r'(\d{4}) (\d{4}) (\d{3})', r'\3 \2 \1', '0530 8023 123')
  14. '123 8023 0530'
  15. >>> re.sub(r'(\d{4}) (?P<prefix>\d{4}) (\d{3})', r'\3 \2 \1 - \g<prefix>', '0530 8023 123')
  16. '123 8023 0530 - 8023'

在一个相同的正则表达式中重用模式

  1. >>> re.match(r'(?P<num>\d{2})(?P=num)', '1212') # 只能匹配相同的内容
  2. >>> print(m) if m is None else m.group()
  3. '1212'
  4. >>> m = re.match(r'(?P<num>\d{2})(?P=num)', '1234') # 不能匹配不同的内容
  5. >>> print(m) if m is None else m.group()
  6. None
  1. >>> bool(re.match(r'\((?P<areacode>\d{3})\) (?P<prefix>\d{3})-(?P<number>\d{4}) (?P=areacode)-(?P=prefix)-(?P=number)',
  2. ... '(800) 555-1212 800-555-1212'))
  3. True
  4. >>> bool(re.match(r'''(?x)
  5. ... # match (800) 555-1212, save areacode, prefix, no.
  6. ... \((?P<areacode>\d{3})\)[ ](?P<prefix>\d{3})-(?P<number>\d{4})
  7. ...
  8. ... # space
  9. ... [ ]
  10. ...
  11. ... # match 800-555-1212
  12. ... (?P=areacode)-(?P=prefix)-(?P=number)
  13. ...
  14. ... # space
  15. ... [ ]
  16. ...
  17. ... # match 18005551212
  18. ... 1(?P=areacode)(?P=prefix)(?P=number)
  19. ... ''', '(800) 555-1212 800-555-1212 18005551212'))
  20. True

前视匹配

可以使用(?=...)(?!...) 符号在目标字符串中实现一个前视匹配,而不必实际使用这些字符串。

  • (?=...) : 正向前视断言
  • (?!...) : 负向前视断言
  1. >>> re.findall(r'\w+(?= van Rossum)', # 正向前视断言
  2. ... '''
  3. ... Guido van Rossum
  4. ... Tim Peters
  5. ... Alex Martelli
  6. ... Just van Rossum
  7. ... Raymond Hettinger
  8. ... ''')
  9. ['Guido', 'Just']
  10. >>> re.findall(r'(?m)^\s+(?!noreply|postmaster)(\w+)', # 负向前视断言
  11. ... '''
  12. ... sales@phptr.com
  13. ... postmaster@phptr.com
  14. ... eng@phptr.com
  15. ... noreply@phptr.com
  16. ... admin@phptr.com
  17. ... ''')
  18. ['sales', 'eng', 'admin']
  19. >>> ['%s@aw.com' % e.group(1) for e in \
  20. ... re.finditer(r'(?m)^\s+(?!noreply|postmaster)(\w+)',
  21. ... '''
  22. ... sales@phptr.com
  23. ... postmaster@phptr.com
  24. ... eng@phptr.com
  25. ... noreply@phptr.com
  26. ... admin@phptr.com
  27. ... ''')]
  28. ['sales@aw.com', 'eng@aw.com', 'admin@aw.com']

条件正则表达式匹配

(?(id/name)yes-pattern|no-pattern)

Will try to match with yes-pattern if the group with given id or name exists, and with no-pattern if it doesn’t. no-pattern is optional and can be omitted. For example, (<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$) is a poor email matching pattern, which will match with '<user@host.com>' as well as 'user@host.com', but not with '<user@host.com' nor 'user@host.com>'.
  1. >>> r = r'(<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$)'
  2. >>> m = re.match(r, '<user@host.com>')
  3. >>> print(m) if m is None else m.group()
  4. '<user@host.com>'
  5. >>> m = re.match(r, 'user@host.com')
  6. >>> print(m) if m is None else m.group()
  7. 'user@host.com'
  8. >>> m = re.match(r, '<user@host.com')
  9. >>> print(m) if m is None else m.group()
  10. None
  11. >>> m = re.match(r, 'user@host.com>')
  12. >>> print(m) if m is None else m.group()

贪婪搜索

加入由一系列类似如下格式的字符串

Thu Feb 15 17:32:12 2007::szhkai@qq.com::1123242-3-5

我们所感兴趣的是,数据记录内包含由连字符连接的三个整数的整行数据

  1. >>> s = '''
  2. Thu Feb 15 17:41:42 2007::szhkai@qq.com::1123242-3
  3. Sun Jul 22 13:32:25 2007::szhkai@qq.com::1123242-5
  4. The May 12 17:02:52 2007::szhkai@qq.com::1123242-3-5
  5. Thu Apr 18 12:22:42 2007::szhkai@qq.com::12323-3-5
  6. '''
  7. >>> re.findall(r'(?m).+\d+-\d+-\d+', s)
  8. ['\tThe May 12 17:02:52 2007::szhkai@qq.com::1123242-3-5', '\tThu Apr 18 12:22:42 2007::szhkai@qq.com::12323-3-5']

如果我们对\d+-\d+-\d+这一部分感兴趣,可以使用元组提取

  1. >>> re.findall(r'(?m).+(\d+-\d+-\d+)', s)
  2. ['2-3-5', '3-3-5']

但是不能提取第一个整数。这是因为正则表达式在实现上是采用贪婪匹配,试图匹配该模式尽可能多的字符。可以使用非贪婪操作符?解决这个问题。可以在*, +, ?后使用?。该操作符要求正则表达式引擎匹配尽可能少的字符。在.+后放置一个?可以获得期望的结果。

  1. >>> re.findall(r'(?m).+?(\d+-\d+-\d+)', s)
  2. ['1123242-3-5', '12323-3-5']

注意事项

ASCII码冲突

如果符号同时使用于ASCII码和正则表达式特殊符号,就会出现问题,如\b表示ASCII字符的退格符,但是\b同时也是一个正则表达式的特殊符号,表示匹配一个单词的边界。对于正则表达式编译器而言,若将\b视为正则表达式特殊字符,需要使用\进行转义。

  1. >>> m = re.match('\bblow', 'blow') # 退格键; 没有匹配
  2. >>> print(m) if m is None else m.group()
  3. None
  4. >>> m = re.match('\\bblow', 'blow') # 匹配单词边界
  5. >>> print(m) if m is None else m.group()
  6. 'blow'
  7. >>> m = re.match(r'\bblow', 'blow') # 使用 raw string
  8. >>> print(m) if m is None else m.group()
  9. 'blow'

w和W字母数字字符集同时受re.L/LOCALEUnicode(re.U/UNICODE)标记影响。

参考

说明

Python版本

  1. # 对于Python2
  2. bovenson@ThinkCentre:~$ python2
  3. Python 2.7.13+ (default, Jul 19 2017, 18:15:03)
  4. [GCC 6.4.0 20170704] on linux2
  5. Type "help", "copyright", "credits" or "license" for more information.
  6. # 对于Python3
  7. bovenson@ThinkCentre:~$ python3
  8. Python 3.5.4rc1 (default, Jul 25 2017, 08:53:34)
  9. [GCC 6.4.0 20170704] on linux
  10. Type "help", "copyright", "credits" or "license" for more information.
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/113719
推荐阅读
相关标签
  

闽ICP备14008679号