[英]Transforming findall regex to finditer
I have following regex 我有以下正则表达式
'\D*(\d{13,19})\D*'
I would like to extract number from following text: 我想从以下文本中提取数字:
Data:
4532394639182605 sadada 4716759060363635 assasasas
dsdsd 4539072249615668 jdhsjhdj
ABCD
When I use this regexp by calling: 当我通过调用使用此正则表达式时:
regex = re.compile('\D*(\d{13,19})\D*')
a = regex.finditer(text)
b = regex.findall(text) #Yield correct result
Results are different- I want result from findall
. 结果是不同的-我想要
findall
结果。 I know that finditer
match group. 我知道那个
finditer
比赛小组。 But how transform my findall regex to finditer regex? 但是,如何将我的findall正则表达式转换为finditer正则表达式?
I need this same result using finditer
like in case use findall
. 我需要使用
finditer
同样的结果,以防万一使用findall
。
tl;dr tl; dr
I want extract numbers not other characters. 我要提取数字而不是其他字符。
finditer
returns match objects from which the groups can be extracted with the .group
method. finditer
返回匹配对象,可以使用.group
方法从中提取.group
。 This is how one usually works with regexes anyway, it's unusual that findall
directly returns strings or tuples with the groups. 无论如何,这通常是使用正则表达式的方式,
findall
直接返回带有组的字符串或元组是不寻常的。
import re
text = '''Data:
4532394639182605 sadada 4716759060363635 assasasas
dsdsd 4539072249615668 jdhsjhdj
ABCD'''
regex = re.compile('\D*(\d{13,19})\D*')
for match in regex.finditer(text):
print(match.group(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.