简体   繁体   English

除了在 python 中的 re.findall() 和 re.finditer() 中返回字符串和迭代器之外,它们的工作方式是否也不同?

[英]Apart from returning string and iterator in re.findall() and re.finditer() in python do their working also differ?

Wrote the following code so that i get all variable length patterns matching str_key.编写了以下代码,以便我获得与 str_key 匹配的所有可变长度模式。

line = "ABCDABCDABCDXXXABCDXXABCDABCDABCD"
str_key = "ABCD"
regex = rf"({str_key})+"

find_all_found = re.findall(regex,line)
print(find_all_found)

find_iter_found = re.finditer(regex, line)
for i in find_iter_found:
    print(i.group())

Output i got: Output 我得到了:

['ABCD', 'ABCD', 'ABCD']
ABCDABCDABCD
ABCD
ABCDABCDABCD

The intended output is last three lines printed by finditer().预期的 output 是 finditer() 打印的最后三行。 I was expecting both functions to give me same output(list or callable does not matter).我期待这两个函数都能给我相同的输出(列表或可调用无关紧要)。 why it differs in findall() as far i understood from other posts already on stackoverflow, these two functions differ only in their return types and not in matching patterns.为什么它在 findall() 中有所不同,据我所知,这两个函数的不同之处仅在于它们的返回类型,而不在于匹配的模式。 Do they work differently, if not what have i done wrong?他们的工作方式是否不同,如果不是我做错了什么?

You want to access groups rather than group .您想访问groups而不是group

>>> find_iter_found = re.finditer(regex, line)
>>> for i in find_iter_found:
...     print(i.groups()[0])

The difference between the two methods is explained here . 这里解释了这两种方法之间的区别。

The behaviour of the two functions is pretty much the same as far as the matching process is concerned as per:就匹配过程而言,这两个函数的行为几乎相同,如下所示:

re.findall(pattern, string, flags=0) re.findall(模式,字符串,标志=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.结果中包含空匹配项。

Changed in version 3.7: Non-empty matches can now start just after a previous empty match.在 3.7 版更改: 非空匹配现在可以在之前的空匹配之后开始。

re.finditer(pattern, string, flags=0) re.finditer(模式,字符串,标志=0)

Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string.返回一个迭代器,该迭代器在字符串中 RE 模式的所有非重叠匹配中产生匹配对象。 The string is scanned left-to-right, and matches are returned in the order found.从左到右扫描字符串,并按找到的顺序返回匹配项。 Empty matches are included in the result.结果中包含空匹配项。

Changed in version 3.7: Non-empty matches can now start just after a previous empty match.在 3.7 版更改: 非空匹配现在可以在之前的空匹配之后开始。

For re.findall change your regex对于re.findall更改您的正则表达式

  • regex = rf"({str_key})+"

into进入

  • regex = rf"((?:{str_key})+)" . regex = rf"((?:{str_key})+)"

The quantifier + have to inside the capture group.量词+必须捕获组内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 re.findall和re.finditer的区别-Python 2.7 re模块中的错误? - Differences in re.findall and re.finditer — bug in Python 2.7 re module? re.finditer 和 re.findall 之间的不同行为 - Different behavior between re.finditer and re.findall 为什么 re.findall() 给我的结果与 Python 中的 re.finditer() 不同? - Why does re.findall() give me different results than re.finditer() in Python? 是否有Perl相当于Python的re.findall / re.finditer(迭代正则表达式结果)? - Is there a Perl equivalent of Python's re.findall/re.finditer (iterative regex results)? 有什么办法可以从bs4 findAll()获得迭代器,例如re.findIter()吗? - Is there any way to get an iterator from bs4 findAll() like re.findIter()? 在正则表达式中使用组时 re.findall() 和 re.finditer() 之间的区别? - Difference between re.findall() and re.finditer() when using groups in regex? 如何在正则表达式上为 re.findall 和 re.finditer 得出相同的结果? - How can I make same result for re.findall and re.finditer on regular expressions? Python re.findall 之后返回带有不需要的字符串的链接 - Python re.findall returning links with unwanted string afterwards 在python中,有没有办法让re.finditer将文件作为输入而不是字符串? - In python, is there a way for re.finditer to take a file as input instead of a string? 无法在 Python 中腌制 `re.finditer` 的 output - Cannot pickle output of `re.finditer` in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM