Below is the code:
import re
a = "111234567890"
b = re.finditer(r"((?<=\d)\d{3})+", a)
print("finditer: ")
print(b)
for item in b:
print(item.group(), end='\n\n')
c = re.findall(r"((?<=\d)\d{3})+", a)
print("findall: ")
print(c, end='\n\n')
d = re.search(r"((?<=\d)\d{3})+", a)
print("search: ")
As shown above, I searched in python3 doc, and found that findall and finditer should both similar to search which check for a match anywhere, not like match just search only at beginning. Thus I user this three to compare, and run code to get below result:
finditer:
<callable_iterator object at 0x000002712064F948>
112345678
findall:
['678']
search:
<re.Match object; span=(1, 10), match='112345678'>
Then my question is:
if my positive lookbehind assertion without "+", which changed to r"((?<=\d)\d{3})" then to match this assertion, result should be 1 digit lookbehind with 3 more digits. Thus:
re.search result should be:123
re.findall result should be:['112', '345', '678']
re.rearch result should be: ['112', '345', '678']
But as my code shown above, I have '+' added after r"((?<=\d)\d{3})", assertion become r"((?<=\d)\d{3})+", that means result should match this (1digt+3digit behind) case for 1 or more time, Then result should combine [112,345,678], As the result shown above:
re.search get the result like'112345678'.
Also re.finditer get the same result as shown above.
However, re.findall get different result as ['678'] shown above.
Can anyone help me to explain why assertion add '+' behind will make re.findall get different result with re.search and re.finditer?
Thanks a lot