python中.find函数的使用方法及实例_Python | findall()和 finditer()用法笔记

re.findall()

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

在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。

string 待匹配的字符串。

pos 可选参数,指定字符串的起始位置,默认为 0。

endpos 可选参数,指定字符串的结束位置,默认为字符串的长度。

实例:

>>> pattern = re.compile(r'\d+') # 查找数字

>>> result1 = pattern.findall('runoob 123 google 456')

>>> print(result1)

['123', '456']

>>> result2 = pattern.findall('run88oob123google456', 0, 10)

>>> print(result2)

['88', '12']

函数释义:

>>> help(re.findall)

Help on function findall in module re:

findall(pattern, string, flags=0)

Return a list of all non-overlapping matches in the string.

If one or more capturing 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.

findall()查询字符串中某个正则表达式模式全部的非重复出现情况。这与 search()在执行字符串搜索时类似,但与 match()和 search()的不同之处在于,findall()总是返回一个列表。

如果 findall()没有找到匹配的部分,就返回一个空列表,但如果匹配成功,列表将包含所有成功的匹配部分(从左向右按出现顺序排列)。

>>> re.findall('car', 'car')

['car']

>>> re.findall('car', 'scary')

['car']

>>> re.findall('car', 'carry the barcardi to the car')

['car', 'car', 'car']

re.finditer()

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

和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。

参数

描述

pattern

匹配的正则表达式

string

要匹配的字符串。

flags

标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。参见:正则表达式修饰符 – 可选标志

实例:

>>> it = re.finditer(r'\d+', '12a32bc43jf3')

>>> for match in it:

print(match.group())

12

32

43

3

函数释义:

>>> help(re.finditer)

Help on function finditer in module re:

finditer(pattern, string, flags=0)

Return an iterator over all non-overlapping matches in the

string. For each match, the iterator returns a Match object.

Empty matches are included in the result.

finditer()函数是在 Python 2.2 版本中添加回来的,这是一个与 findall()函数类似但是更节省内存的变体。两者之间以及和其他变体函数之间的差异(很明显不同于返回的是一个迭代器还是列表)在于,和返回的匹配字符串相比,finditer()在匹配对象中迭代。

如下是在单个字符串中两个不同分组之间的差别:

>>> s = 'This and that.'

>>> re.findall(r'(th\w+) and (th\w+)', s, re.I)

[('This', 'that')]

>>> re.finditer(r'(th\w+) and (th\w+)', s,re.I).__next__().groups()

('This', 'that')

>>> re.finditer(r'(th\w+) and (th\w+)', s,re.I).__next__().group(1)

'This'

>>> re.finditer(r'(th\w+) and (th\w+)', s,re.I).__next__().group(2)

'that'

>>> [g.groups() for g in re.finditer(r'(th\w+) and (th\w+)',s, re.I)]

[('This', 'that')]

类似写法:

>>> for g in re.finditer(r'(th\w+) and (th\w+)',s, re.I):

print(g.groups())

('This', 'that')

如下是单个字符串中执行单个分组的多重匹配:

>>> s = 'This and that.'

>>> it = re.finditer(r'(th\w+)', s, re.I)

>>> g = it.__next__()

>>> g.groups()

('This',)

>>> g.group()

'This'

>>> g = it.__next__()

>>> g.groups()

('that',)

>>> g.group(1)

'that'

>>> [g.group(1) for g in re.finditer(r'(th\w+)', s, re.I)]

['This', 'that']

注意:

使用 finditer()函数完成的所有额外工作都旨在获取它的输出来匹配 findall()的输出。

最后 ,与 match()和 search()类似,findall()和 finditer()方法的版本支持可选的 pos 和 endpos参数,这两个参数用于控制目标字符串的搜索边界,这与本章之前的部分所描述的类似。

引申:分组的深入理解

先看下案例

import re

phoneRegex = re.compile(r'''( # 第一行 (group1

(\d{3}|\(\d{3}\))? # 区号 (group2)

(\s|-|\.)? # 分隔符 (group3)

(\d{3}) # 前3个数字 (group4)

(\s|-|\.) # 分隔符 (group5)

(\d{4}) # 后4个数字 (group6)

(\s*(ext|x|ext.)\s*(\d{2,5}))? # 扩展名 (group7(group8)(group9))

)''', re.VERBOSE) # 自后一行 )

text = '(111)-222-3333 x 444'

lp = phoneRegex.findall(text)

print(lp)

输出结果为:

[('(111)-222-3333 x 444', '(111)', '-', '222', '-', '3333', ' x 444', 'x', '444')]

这是一个含有1个元组的列表,这1个元组中包含9个元素,即为compile的9个小括号,也即9个group(),见代码注释。

>>> phoneRegex = re.compile(r'''( # 第一行 (group1

(\d{3}|\(\d{3}\))? # 区号 (group2)

(\s|-|\.)? # 分隔符 (group3)

(\d{3}) # 前3个数字 (group4)

(\s|-|\.) # 分隔符 (group5)

(\d{4}) # 后4个数字 (group6)

(\s*(ext|x|ext.)\s*(\d{2,5}))? # 扩展名 (group7(group8)(group9))

)''', re.VERBOSE) # 自后一行 )

>>> re.search(phoneRegex, '(111)-222-3333 x 444')

>>> lp = re.search(phoneRegex, '(111)-222-3333 x 444')

>>> lp.group()

'(111)-222-3333 x 444'

>>> lp.group(1)

'(111)-222-3333 x 444'

>>> lp.group(2)

'(111)'

>>> lp.group(3)

'-'

>>> lp.group(4)

'222'

>>> lp.group(5)

'-'

>>> lp.group(6)

'3333'

>>> lp.group(7)

' x 444'

>>> lp.group(8)

'x'

>>> lp.group(9)

'444'

>>> lp.group(10)

Traceback (most recent call last):

File "", line 1, in

IndexError: no such group

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值