Python正则表达式之findall和finditer函数

By | February 26, 2018
#!/usr/bin/python
#-*- coding:utf-8 -*-

import re
# findall函数
'''
findall(pattern, string, flags=0) 
返回所有匹配到的项的列表
'''
print re.findall('car', 'car') # ['car']
print re.findall('car', 'scary') # ['car']
print re.findall('car', 'carry the barcardi to the car') # ['car', 'car', 'car']
print re.findall('car', 'carry the barcardi car to the car') # ['car', 'car', 'car', 'car']

## 有子组的情况下
s = 'This and that.'
s2 = 'This and that.These and those.'
print re.findall(r'(th\w+) and (th\w+)', s, re.I) # [('This', 'that')]
print re.findall(r'(Th\w+) and (th\w+)', s2, re.I) # [('This', 'that'), ('These', 'those')]

# finditer函数
'''
不同于 findall 函数,
返回的是迭代器,每次迭代返回一个匹配到的匹配对象
'''
print re.finditer(r'(th\w+) and (th\w+)',s, re.I) # <callable-iterator object at 0x7f29a6df1450>
itera = re.finditer(r'(th\w+) and (th\w+)',s, re.I)
print itera.next().groups() # ('This', 'that')
itera = re.finditer(r'(th\w+) and (th\w+)',s2, re.I)
print itera.next().groups() # ('This', 'that')
print itera.next().groups() # ('These', 'those')
# print itera.next().groups() # StopIteration 异常

print [g.groups() for g in re.finditer(r'(th\w+) and (th\w+)', s, re.I)] # [('This', 'that')]
print [g.groups() for g in re.finditer(r'(th\w+) and (th\w+)', s2, re.I)] # [('This', 'that'), ('These', 'those')]


print [g.group(1) for g in re.finditer(r'(th\w+) and (th\w+)', s, re.I)] # ['This']
print [g.group(1) for g in re.finditer(r'(th\w+) and (th\w+)', s2, re.I)] # ['This', 'These']

## 如果以上的函数是正则表达式对象的方法,那么就还有 pos 和 endpos 两个可选参数。

Created On: 2018-02-26

鉴于本人的相关知识储备以及能力有限,本博客的观点和描述如有错漏或是有考虑不周到的地方还请多多包涵,也欢迎指正,一起学习,共同进步。如果本文对您有帮助,而且让您觉得值得为内容付费,那么就请赞助(打赏)一下本人,这不强制。打赏支持微信支付,方法是使劲地戳一下下方的“打赏”按钮,然后得到微信收款的二维码,再用微信支付扫一下,就像买菜那样。祝好!