python学习-Day23-re的相关方法

来源:互联网 发布:国外社交网络发展历程 编辑:程序博客网 时间:2024/06/15 07:04

re的match方法:

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

string:匹配使用的文本,

pos: 文本中正则表达式开始搜索的索引。及开始搜索string的下标

endpos: 文本中正则表达式结束搜索的索引。

如果不指定pos,默认是从开头开始匹配,如果匹配不到,直接返回None

#@File :demo_re.pyimport rereg = re.compile(r'(hello w.*)(hello x.*)')reg_kuohao = re.compile(r'((hello w.*)(hello x.*))')b = 'rehello world hello xiao'a = 'hello world hello xiao'result = reg.match(a)results = reg_kuohao.match(a)bad_result = reg.match(b)print(bad_result)print('#################################################')print(result)print(dir(reg))print(result.groups())print(results.groups())
None
#################################################
<_sre.SRE_Match object at 0x02CADD58>
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'findall', 'finditer', 'flags', 'groupindex', 'groups', 'match', 'pattern', 'scanner', 'search', 'split', 'sub', 'subn']
('hello world ', 'hello xiao')
('hello world hello xiao', 'hello world ', 'hello xiao')


re的search方法:

这里是search的使用方法,也是与match进行比较。
关于search:

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

这个方法用于查找字符串中可以匹配成功的子串。从string的pos下标处起尝试匹配pattern,如果pattern结束时仍可匹配,则返回一个Match对象;若无法匹配,则将pos加1后重新尝试匹配;直到pos=endpos时仍无法匹配则返回None。

result_search = reg.search(b)\\这里是跟上面的例子一起的。前面就省略了。print(result_search)print(result_search.groups())
<_sre.SRE_Match object at 0x036D4890>
('hello world ', 'hello xiao')

由以上对比可知,由于search是进行多次的查询匹配。所以可以知道,两者的性能方面肯定是match较好。并且,在两者匹配字符串的区别,可以使用其他方法规避。比如,使用match的时候可以与其他正则方法一起使用达到性能与要求同时达到的效果!

reg = re.compile(r'\w*(hello w.*)(hello x.*)')\\将最开始的程序按如此修改,match最终效果等于search
这样在定义正则对象时,不仅要实现效果还要尽量达到最优!才是一个合格的程序员。


re的split方法:正则对象的split方法,使用正则匹配进行分割字符串

split(string[, maxsplit])

按照能够匹配的子串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定将全部分割

p = re.compile(r'\d+')a_str = 'one33two443three223four'print(p.split(a_str))

['one', 'two', 'three', 'four']

解释:直接把p的正则当成是分隔符,然后把最后的字符串用p进行分割,最后是以列表的形式返回回去。


re的findall方法:正则对象使用的findall方法,来查找符合对象的字符串

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

搜索string,以列表形式返回全部能匹配的子串

print(p.findall(a_str))
['33', '443', '223']

最后是以列表的形式返回回去。

re的finditer方法:

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

搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。

for i in p.finditer(a_str):    print(i.group())
33
443
223

解释:

p.finditer('one1two2three3four4')是一个迭代器,而返回的每个m都是match对象,group方法也会在下一节进行详细介绍。


re的match匹配对象:

Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读属性或方法来获取这些信息。上面的过程中多次使用了match对象,调用了他的group()和groups()等方法。

#@File :re_match.pyimport reprog = re.compile(r'(?P<tagname>abc)(.*)(?P=tagname)')result1 = prog.match('abclfjlad234sjldabc')print(result1)print(result1.groups())print result1.group('tagname')print(result1.group(2))\\match对象的group返回一个元组,下标是以1开头的。print(result1.groupdict())\\有分组名的,以字典形式返回。
<_sre.SRE_Match object at 0x02F43D10>
('abc', 'lfjlad234sjld')
abc
lfjlad234sjld
{'tagname': 'abc'}

解释:

1,  我们可以看到result1已经由字符串转换成了一个正则对象。

2,  resule.groups()可以查看出来所有匹配到的数据,每个()是一个元素,最终返回一个tuple

3,  group()既可以通过下标(从1开始)的方式访问,也可以通过分组名进行访问。

4,  groupdict只能显示有分组名的数据








原创粉丝点击