[英]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.