您的位置:首页 > 编程语言 > Python开发

python正则表达式——re模块

2017-12-21 13:37 337 查看
尊重原创,原文地址:http://blog.csdn.net/u014683535/article/details/51872093
http://blog.csdn.net/lisonglisonglisong/article/details/39697137
本文所有的代码使用的python版本为python3.5.1,运行环境为Ubuntu 16.04 LTS, GCC 5.3.1.

本文参考的python文档版本为python3.5.2,强烈建议英语不错的同学直接读文档。这篇博客只是作者阅读文档时的学习笔记,若有纰漏请指出,让我们共同进步。


正则表达式基础


介绍正则表达式

正则表达式re是python自带的模块,但是正则表达式并不是python的一部分,它有自己独特的语法和自己独立的编译引擎,效率比str自带的方法低,但是功能强大。这块建议看这篇博客——python正则表达式指南,画的图很明了。

正则表达式的语法一般来说都是适用的,请参考正则表达式30 分钟入门python文档或者上边推荐的博客python正则表达式指南


贪婪模式和非贪婪模式:

一般情况下是默认为贪婪模式,这种情况下匹配尽可能多的字符。非贪婪模式则相反,匹配尽可能少的字符。如:
In [2]: import re

In [3]: pt = re.compile(r'ab*?') #非贪婪模式

In [4]: pt.match("abbbc")
Out[4]: <_sre.SRE_Match object; span=(0, 1), match='a'>

In [5]: pt2 = re.compile(r'ab*') #贪婪模式

In [7]: pt2.match("abbbc")
Out[7]: <_sre.SRE_Match object; span=(0, 4), match='abbb'>
1
2
3
4
5
6
7
8
9
10
11
12
[/code]


使用python raw字符串

在python row字符串中,所有的字符都是字面意思,没有转义效果。python的普通字符串str中
\
有转义作用,而在正则表达式中
\
同样有额外的作用。所以我们为了在正则表达式中使用
\
必须要在str字符串中使用
\\
或者在raw字符串中使用
\
。而如果想在正则表达式中输入
\
字符,则更是要输入
\\\\
,每两个
\\
在编程语言中转义为
\
,然后两个
\\
在正则表达式中转义为一个
\
。这就太麻烦了。

总之呢,就是如果不使用raw字符串,就需要先解决str字符串中的
\
转义问题,然后再考虑正则表达式中
\
转义问题。所以不如直接就在正则表达式中使用raw字符串,这样只需要考虑正则表达式中的转义问题。


re模块


re.compile(pattern, flags=0)

re.compile用来编译正则表达式模式字符串,并生成Regular Expression Objects
compile(pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.
1
2
[/code]

flags有很多可选值:

re.I(IGNORECASE)
忽略大小写,括号内是完整的写法
re.M(MULTILINE)
多行模式,改变
^
$
的行为
re.S(DOTALL)
点可以匹配任意字符,包括换行符
re.L(LOCALE)
做本地化识别的匹配,不推荐使用
re.U(UNICODE)
使用\w \W \s \S \d \D使用取决于unicode定义的字符属性。在python3中默认使用该flag
re.X(VERBOSE)
冗长模式,该模式下pattern字符串可以是多行的,忽略空白字符,并可以添加注释

In [8]: pt = re.compile('''\d #fist line''', re.X)

In [9]: pt
Out[9]: re.compile(r'\d #fist line', re.UNICODE|re.VERBOSE)

In [10]: pt.match('123')
Out[10]: <_sre.SRE_Match object; span=(0, 1), match='1'>
1
2
3
4
5
6
7
[/code]


re.match(pattern, string, flags=0)

match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
1
2
3
[/code]

re.match()只匹配位于字符串开始位置的模式串.如果想要匹配任意位置的模式串,可以使用re.search().例如:
In [8]: import re

In [9]: m = re.match('c', 'abcdef') #不匹配,返回None

In [10]: type(m)
Out[10]: NoneType

In [11]: m = re.match('a', 'abcdef') #匹配,返回match对象

In [12]: m
Out[12]: <_sre.SRE_Match object; span=(0, 1), match='a'>
1
2
3
4
5
6
7
8
9
10
11
[/code]


re.fullmatch(pattern, string, flags=0)

fullmatch(pattern, string, flags=0)
Try to apply the pattern to all of the string, returning
a match object, or None if no match was found.
1
2
3
[/code]

re.fullmatch()返回一个和模式串完全匹配的字符串例如:
In [9]: m1 = re.fullmatch('a', 'abcdef') #不完全匹配,返回None

In [10]: m1

In [11]: type(m1)
Out[11]: NoneType
1
2
3
4
5
6
[/code]


re.search(pattern, string, flags=0)

search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.
1
2
3
[/code]

re.search()搜索所有位置匹配的模式串,但是只返回第一个匹配到的例如:
In [1]: import re

In [2]: m1 = re.search('c','abcdef') #匹配,返回match对象

In [3]: m1
Out[3]: <_sre.SRE_Match object; span=(2, 3), match='c'>

In [4]: m2 = re.search('c', 'abcdefcg') #只匹配第一个c

In [5]: m2
Out[5]: <_sre.SRE_Match object; span=(2, 3), match='c'>

In [6]: m3 = re.search('z', 'abcdefg') #不匹配,返回None

In [7]: type(m3)
Out[7]: NoneType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[/code]


re.split(pattern, string,
maxsplit=0, flags=0)

Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.
1
[/code]

re.split()按照pattern的出现来分割string。如果pattern被括号包围,则将pattern也放在结果的list里返回。如果maxsplit不是0,则最多maxsplit次分割,其余部分作为一个整体放到结果list的最后

例如:
In [17]: re.split('\W+', 'hello, world, use - python')
Out[17]: ['hello', 'world', 'use', 'python']

In [18]: re.split('(\W+)', 'hello, world, use - python') #pattern也在结果列表里
Out[18]: ['hello', ', ', 'world', ', ', 'use', ' - ', 'python']

In [19]: re.split('(\W+)', 'hello, world, use - python', 1)   #maxsplit=1,最多分割1次
Out[19]: ['hello', ', ', 'world, use - python']

In [20]: re.split('([a-z]+)', '0Hello1world2use3Python', flags=re.I)   #flags=re.I,pattern匹配时忽略字符串的大小写
Out[20]: ['0', 'Hello', '1', 'world', '2', 'use', '3', 'Python', '']

In [21]: re.split('[a-z]+', '0Hello1world2use3Python', flags=re.I)
Out[21]: ['0', '1', '2', '3', '']

In [22]: re.split('[a-z]+', 'Say0Hello1world2use3Python', flags=re.I)   #注意如果pattern在string的开头或结尾,resultlist中添加''在相应位置
Out[22]: ['', '0', '1', '2', '3', '']

# 当pattern是空的时候,匹配会被忽略。不会忽略的做法将在未来的python中实现,还是会抛出FutureWarning
In [58]: re.split('x*', 'axbc')
/usr/lib/python3.5/re.py:203: FutureWarning: split() requires a non-empty pattern match.
return _compile(pattern, flags).split(string, maxsplit)
Out[58]: ['a', 'bc']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[/code]


re.findall(pattern, string, flags=0)

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.
1
[/code]

re.findall()按照在string里的顺序从左到右返回所有不重叠的匹配串。
In [24]: re.findall('a', 'bcd') #没有匹配,返回空列表
Out[24]: []

In [25]: re.findall('[a-n]', 'paulzheng')  #按照从左到右的顺序返回匹配值
Out[25]: ['a', 'l', 'h', 'e', 'n', 'g']

# 空的模式串也会被匹配,除非他们和下一个匹配串的开头相连。
In [59]: re.findall('x*', 'axbc')
Out[59]: ['', 'x', '', '', ''] #四个空串分别匹配a前,b前,c前,c后。 x前的被忽略。
1
2
3
4
5
6
7
8
9
10
[/code]


re.finditer(pattern, string, flags=0)

Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match.
1
[/code]

re.finditer()返回一个迭代器
iterator
,这个iterator yield match objects.返回顺序、内容和re.findall()相同

例如:
In [26]: re.finditer('[a-n]', 'paulzheng')
Out[26]: <callable_iterator at 0x7f6c58629e10>

In [27]: g = re.finditer('[a-n]', 'paulzheng') #g是迭代器

In [28]: next(g)
Out[28]: <_sre.SRE_Match object; span=(1, 2), match='a'>

In [29]: next(g)
Out[29]: <_sre.SRE_Match object; span=(3, 4), match='l'>

In [30]: next(g)
Out[30]: <_sre.SRE_Match object; span=(5, 6), match='h'>

In [31]: next(g)
Out[31]: <_sre.SRE_Match object; span=(6, 7), match='e'>

In [32]: next(g)
Out[32]: <_sre.SRE_Match object; span=(7, 8), match='n'>

In [33]: next(g)
Out[33]: <_sre.SRE_Match object; span=(8, 9), match='g'>

In [34]: next(g)   # 没有内容可以yield
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-34-5f315c5de15b> in <module>()
----> 1 next(g)

StopIteration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[/code]


re.sub(pattern, repl,
string, count, flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl.  repl can be either a string or a callable; if a string, backslash escapes in it are processed.  If it is a callable, it's passed the match object and must return a replacement string to be used.
1
[/code]

re.sub()按照从左到右扫描不重叠的匹配串,并用repl替换。repl可以是一个字符串,或者是一个函数(callbale,可调用对象)。如果repl是字符串,则有转义效果;如果是一个函数,那么它参数是match对象,返回一个用于替代的字符串。pattern可以是一个字符串,也可以是一个RE object(即上边的re.compile()返回的对象)

例如:
In [54]: re.sub(r'([a-h])([a-h])', r'\1', 'pabulzhiefngh')  # \1和\g<1>语义相同
Out[54]: 'paulzhieng'

#repl是函数
In [65]: def dashrepl(matchobj):
....:     if matchobj.group(1) == 'a': return ' '
....:     else: return '-'
....:

In [66]: re.sub(r'([a-h])([a-h])', dashrepl, 'pabulzhiefngh')
Out[66]: 'p ulzhi-n-'

#当模式串是空串时,仅当它和前一个匹配串不相连才会匹配该空串。
In [67]: re.sub('x*', '-', 'axbc')
Out[67]: '-a-b-c-'   #四个'-'分别匹配a前,a后,b后,c后。x后的被忽略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[/code]


re.subn(pattern, repl, string, count=0, flags=0)

re.subn()执行和re.sub()一样的操作,返回一个tuple (new_string, number_of_subs_made)

例如:
In [65]: def dashrepl(matchobj):
....:     if matchobj.group(1) == 'a': return ' '
....:     else: return '-'
....:

In [68]: re.subn(r'([a-h])([a-h])', dashrepl, 'pabulzhiefngh')
Out[68]: ('p ulzhi-n-', 3) # 3是替换的数目
1
2
3
4
5
6
7
[/code]


re.error(msg, pattern=None, pos=None)

当正则表达式编译错误或者匹配时发生错误返回re.error,但永远不可能因为没有匹配而产生re.error


Regular Expression Objects

Regular Expression Objects指的是re.compile()返回的对象,以下简称RE对象。以下所用的
regex
为RE对象


regex.search(string [, pos[, endpos]])

search(string=None, pos=0, endpos=9223372036854775807, *, pattern=None) method of _sre.SRE_Pattern instance
Scan through string looking for a match, and return a corresponding match object instance.

Return None if no position in the string matches.
(END)
1
2
3
4
5
[/code]

regex.search返回第一个匹配的match对象。从pos到endpos往右搜索,默认pos=0, endpos=字符串末尾。

例如:
In [76]: regex = re.compile('a') # 创造一个RE对象

In [77]: regex.search('bacd')   #匹配成功,返回一个Match对象
Out[77]: <_sre.SRE_Match object; span=(1, 2), match='a'>

In [78]: regex.search('bacd', 2) #匹配失败,返回None

In [79]: regex = re.compile('^a') #这个RE对象从string的开始位置匹配

In [80]: regex.search('bacd') #不匹配

In [81]: regex.search('bacd', 1) #不匹配,说明^依旧表示从新的一行开始匹配而不是从pos开始匹配
1
2
3
4
5
6
7
8
9
10
11
12
[/code]

注意会匹配空串
In [1]: import re

In [2]: pattern = re.compile('x*')

In [3]: pattern.search('axbc')
Out[3]: <_sre.SRE_Match object; span=(0, 0), match=''> # 模式串为空
1
2
3
4
5
6
[/code]


regex.match(string [, pos [, endpos]])

match(string=None, pos=0, endpos=9223372036854775807, *, pattern=None) method of _sre.SRE_Pattern instance
Matches zero or more characters at the beginning of the string.
1
2
[/code]

regex.match()匹配string从pos开始位置的0个或多个字符串。pos和endpos作用同regex.search()相同。如果想匹配任意位置的匹配串,可以使用regex.search.

例如:
In [8]: regex1 = re.compile('\d')  #编译正则表达式创造RE object

In [9]: regex1.match('1hello')  #pos=0, 匹配index=0处的1
Out[9]: <_sre.SRE_Match object; span=(0, 1), match='1'>

In [10]: regex1.match('hello2',5) #pos=5,匹配index=5处的2
Out[10]: <_sre.SRE_Match object; span=(5, 6), match='2'>

In [11]: regex2 = re.compile('\d+') # 创建regex2

In [12]: regex2.match('12345',1,3) # 匹配[1:3]位置的string
Out[12]: <_sre.SRE_Match object; span=(1, 3), match='23'>

In [14]: regex3 = re.compile('x*') #创建regex3

In [15]: regex3.match('axbc')  #匹配index=0空串
Out[15]: <_sre.SRE_Match object; span=(0, 0), match=''>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[/code]


regex.fullmatch(string [, pos [, endpos]])

fullmatch(string=None, pos=0, endpos=9223372036854775807, *, pattern=None) method of _sre.SRE_Pattern instance
Matches against all of the string
1
2
[/code]

regex匹配整个字符串。例子:
In [21]: regex = re.compile('\d+') # 编译正则表达式,创造RE对象

In [22]: regex.fullmatch('12345')  # 匹配整个string成功
Out[22]: <_sre.SRE_Match object; span=(0, 5), match='12345'>

In [23]: regex.fullmatch('12345hello') # 不匹配整个string,返回None

In [24]: regex.fullmatch('hello12345world',5,10) # 匹配整个string[5:10]成功
Out[24]: <_sre.SRE_Match object; span=(5, 10), match='12345'>
1
2
3
4
5
6
7
8
9
[/code]


regex.split(string, maxsplit=0)

split(string=None, maxsplit=0, *, source=None) method of _sre.SRE_Pattern instance
Split string by the occurrences of pattern.
1
2
[/code]

regex.split()和re.split()完全相同


regex.findall(string [, pos [, endpos]])

findall(string=None, pos=0, endpos=9223372036854775807, *, source=None) method of _sre.SRE_Pattern instance
Return a list of all non-overlapping matches of pattern in string.
1
2
[/code]

regex.findall()和re.findall()作用相同,但是可以指定pos, endpos(pos和endpos作用和regex.search()相同)


regex.finditer(string [, pos [, endpos]])

regex.finditer()和re.finditer()作用相同,但是可以指定pos, endpos(pos和endpos作用和regex.search相同)


regex.sub(repl, string, count=0)

regex.sub()和re.sub作用完全相同


regex.subn(repl,string,count=0)

regex.subn()和re.subn作用完全相同


RE对象的属性


regex.flags

即编译该RE对象时re.compile()的flags或者正则表达式中的(?…)


regex.groups

正则表达式的group数


regex.groupindex

正则表达式中
(?P<name>)
name和group下标的字典。如果正则表达式中没有用到
(?P<name>)
则字典为空


regex.pattern

RE对象的正则表达式

例子:
In [29]: regex = re.compile('(?P<first>hello),(?P<second>world)')

In [30]: regex.groups
Out[30]: 2

In [31]: regex.groupindex
Out[31]: mappingproxy({'second': 2, 'first': 1})

In [32]: regex.pattern
Out[32]: '(?P<first>hello),(?P<second>world)'

In [33]: regex.flags
Out[33]: 32   #flag=re.UNICODE
1
2
3
4
5
6
7
8
9
10
11
12
13
[/code]


Match Objects

match objects的
boolean
值为
True
,可以用
if
来判断是否有match
对象
match = re.search(pattern, string)
if match:
process(match)
1
2
3
[/code]


match.expand(template)

expand(template) method of _sre.SRE_Match instance
Return the string obtained by doing backslash substitution on the string template, as done by the sub() method.
1
2
[/code]

match.expand()用match对象的字符串中的分组替换掉template中的
\1
\g<1>
\g<name>
,和re.sub()的做法相同。

例如:
In [29]: regex = re.compile('(?P<first>hello),(?P<second>world)')  #两个分组

In [34]: match = re.search(regex, 'hello,world')

In [35]: match
Out[35]: <_sre.SRE_Match object; span=(0, 11), match='hello,world'>  #匹配成功

In [36]: match.expand(r'\1----\2')   # 用hello和world替换掉`\1`和`\2`
Out[36]: 'hello----world'
1
2
3
4
5
6
7
8
9
[/code]


match.group([group1,…])

group(...) method of _sre.SRE_Match instance
group([group1, ...]) -> str or tuple.
Return subgroup(s) of the match by indices or names.
For 0 returns the entire match.
1
2
3
4
[/code]

match.group()返回一个字符串或者一个元组。如果参数为空或为0,则返回整个match的匹配串。如果参数为[1…99]则返回对应组的字符串。如果不存在对应组则产生
IndexError
。当有多个参数时,返回一个元组包含对应的字符串。

例如:
In [49]: regex = re.compile('(?P<first>hello)~(?P<second>world)')

In [50]: match = re.search(regex, 'hello~world')

In [51]: match
Out[51]: <_sre.SRE_Match object; span=(0, 11), match='hello~world'>

In [52]: match.group()  #参数为空,返回整个匹配串
Out[52]: 'hello~world'

In [53]: match.group(0)  #参数为0,返回整个匹配串
Out[53]: 'hello~world'

In [54]: match.group(1) #参数为1,返回第1个组对应的字符串
Out[54]: 'hello'

In [55]: match.group(2)  #参数为2,返回第2个组对应的字符串
Out[55]: 'world'

In [56]: match.group(3)  #不存在对应的组,产生`IndexError`
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-56-da7df187e689> in <module>()
----> 1 match.group(3)

IndexError: no such group

In [57]: match.group('first')  #使用`name`来访问对应的组
Out[57]: 'hello'

In [58]: match.group('second')  #使用`name`来访问对应的组
Out[58]: 'world'

In [59]: match.group(1,2)   #多个参数,返回一个tuple
Out[59]: ('hello', 'world')

In [60]: match.group(0,1,2)
Out[60]: ('hello~world', 'hello', 'world')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[/code]

当一个组的模式串可以匹配多次时,只可以访问最后一次的匹配串
In [63]: m = re.match(r'(..)+', "a1b2c3")

In [64]: m.group(1)  # 匹配3次,分别是a1,b2,c3,但是只可以访问最后一次
Out[64]: 'c3'

In [65]: m.group()
Out[65]: 'a1b2c3'
1
2
3
4
5
6
7
[/code]


match.groups(default=None)

groups(default=None) method of _sre.SRE_Match instance
Return a tuple containing all the subgroups of the match, from 1.

default
Is used for groups that did not participate in the match.
1
2
3
4
5
[/code]

match.groups()返回一个元组包含从1开始所有的group,例如:
In [67]: regex = re.compile('(?P<first>hello)~(?P<second>world)')

In [68]: match = regex.search('hello~world')

In [69]: match.groups()
Out[69]: ('hello', 'world') #返回match的两个分组
1
2
3
4
5
6
[/code]

如果模式串中的分组是可选的,则在结果中的分组的匹配串是default的值(默认是None)
In [75]: regex = re.compile(r'(?P<first>hello)~(?P<second>world)-?(?P<name>\w+)?')

In [76]: match = regex.search('hello~world')

In [77]: match.groups() # name分组没有匹配
Out[77]: ('hello', 'world', None) # name分组为None

In [78]: match.groups('paul')  # default为'paul'
Out[78]: ('hello', 'world', 'paul') #name分组为'paul'
1
2
3
4
5
6
7
8
9
[/code]


match.groupdict(default=None)

groupdict(default=None) method of _sre.SRE_Match instance
Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.

default
Is used for groups that did not participate in the match.
1
2
3
4
5
[/code]

match.groupdict()返回一个字典,key是分组的名字,值为对应的分组匹配串。如果分组没有匹配,值为default,用法与match.groups()相同.例如:
In [80]: regex = re.compile(r'(?P<first>hello)~(?P<second>world)-?(?P<name>\w+)?')

In [81]: match = regex.search('hello~world')

In [82]: match.groupdict() # name没有匹配对应的值为None
Out[82]: {'first': 'hello', 'name': None, 'second': 'world'}
1
2
3
4
5
6
[/code]


match.start([group])

start(group=0, /) method of _sre.SRE_Match instance
Return index of the start of the substring matched by group.
1
2
[/code]


match.end([group])

end(group=0, /) method of _sre.SRE_Match instance
Return index of the end of the substring matched by group.
1
2
[/code]

match.start()和match.end()分别返回分组的匹配串的开始index和末尾index。参数默认为0。如果参数对应的分组没有匹配,则返回-1。
match.group(g)相当于
string[match.start(g): match.end(g)]
1
2
[/code]


match.span([group])

span(group=0, /) method of _sre.SRE_Match instance
For MatchObject m, return the 2-tuple (m.start(group), m.end(group)).
1
2
[/code]

match.span(group)返回
(match.start(group), match.end(group))
.参数默认为0。如果group未匹配,则返回(-1, -1).

例如:
In [85]: regex = re.compile(r'(?P<first>hello)~(?P<second>world)-?(?P<name>\w+)?')

In [86]: match = regex.search('hello~world')

In [87]: match.group(1)
Out[87]: 'hello'

In [88]: match.start(1)  # 第1组匹配串的开始下标
Out[88]: 0

In [89]: match.end(1)  # 第1组匹配串的结束下标
Out[89]: 5

In [90]: match.start(0)  # 第0组即整个匹配串的开始下标
Out[90]: 0

In [91]: match.end(0)   # 第0组即整个匹配串的结束下标
Out[91]: 11

In [92]: match.group(0)
Out[92]: 'hello~world'

In [94]: match.span(1)   #返回第一组的开始下标和结束下标
Out[94]: (0, 5)

In [95]: match.span(3)   #返回第三组的开始和结束下标
Out[95]: (-1, -1)

In [96]: match.span("second")  # 用名字作参数,返回第二组的开始和结束下标
Out[96]: (6, 11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[/code]


Match Objects的属性


match.pos

传给regex.search()regex.match()的pos参数


match.endpos

传给regex.search()regex.match()的endpos参数


match.lastindex

最后一个匹配的分组下标。如果没有分组返回None


match.lastgroup

最后一个匹配的分组名字,如果这个分组没有名字返回None


match.re

match的RE对象


match.string

match的整个查找串(search()match()的参数)

例如:
In [105]: regex = re.compile(r'(?P<first>hello)~(?P<second>world)-?(?P<name>\w+)?')

In [106]: match = regex.search('hello~world')
In [107]: match.pos  # pos默认是0
Out[107]: 0

In [108]: match.endpos   #endpos默认是总长
Out[108]: 11

In [109]: match.groups()   #总共三个组,但是第3个没有匹配
Out[109]: ('hello', 'world', None)

In [110]: match.lastindex   #最后一个匹配的组的下标为2
Out[110]: 2

In [111]: match.lastgroup   #最后一个匹配的组的名字为second
Out[111]: 'second'

In [112]: match.re   # match的RE对象
Out[112]: re.compile(r'(?P<first>hello)~(?P<second>world)-?(?P<name>\w+)?', re.UNICODE)

In [113]: type(match.re)
Out[113]: _sre.SRE_Pattern

In [114]: match.string   #match查找的字符串
Out[114]: 'hello~world'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[/code]


技巧总结


re.search() vs regex.search()

re.search()可以节省一行re.compile()的代码,但是依然依然会有RE 对象,就是re.search()的pattern编译生成的
。而且re.search()的pattern可以是正则表达式字符串,也可以是RE对象。

这些特点同理可推及re.match()vsregex.match(), re.fullmatch()vsregex.fullmatch(), re.split()vsregex.split(), re.findall()vsregex.findall(), re.finditer()vsregex.finditer(), re.sub()vsregex.sub(), re.subn()vsregex.subn()


使用raw strings来表示正则表达式

建议使用raw字符串,本文代码未使用是因为似乎python3中默认使用raw字符串
In [11]: regex = re.compile('\d')

In [12]: regex
Out[12]: re.compile(r'\d', re.UNICODE)  #默认使用了raw strings
1
2
3
4
5
[/code]


空串的匹配

只有在re.split()中空模式串会被忽略(然而这一特性将在未来未来版本中修正,但会抛出FutureError)

re.findall()中只有当空串不和下一个匹配串相连时才会匹配

re.sub()中只有当空串不和前一个匹配串相连时才会匹配

regex.split(), regex.findall(), regex.sub()同理

其余情况空串和普通串完全相同。


pos和endpos都和str切片的下标意义相同

即如果想要表示到字符串末尾应该endpos是len(str),而不是str最后一个字符的下标len(str)-1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python