The following code iterates over an object created by
matches = pattern.finditer(text_to_search)
Later I try to view this object as a list, however this list is empty. When I put this request before iterating, I get the correct list. Then the iteration itself stops working and the results are not shown. I think there is something similar to reading a file here, but then you can use the .seek() method to come back to the beginning. I don't know what to do with the calleble iterator, same method doesn't apply here. Sorry for the poor explanation of the problem, I don't know how to describe it better.
import re
text_to_search = '''
abcdefghijklmnopqurtuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1234567890
Ha HaHa
MetaCharacters (Need to be escaped):
. ^ $ * + ? { } [ ] \ | ( )
coreyms.com
321-555-4321
123.555.1234abc
123*555*1234
800-555-1234
900-555-1234
Mr. Schafer
Mr Smith
Ms Davis
Mrs. Robinson
Mr. T
'''
sentence = 'Start a sentence and then bring it to an end'
pattern = re.compile(r'abc')
matches = pattern.finditer(text_to_search)
for match in matches:
print(match)
print(matches)
print(list(matches))
code result:
<re.Match object; span=(1, 4), match='abc'>
<re.Match object; span=(180, 183), match='abc'>
<callable_iterator object at 0x00000269F1DD5390>
[]
!! list is empty !!
code modification for the test:
print(list(matches)) #this line of code was moved up
for match in matches:
print(match)
print(matches)
#from there
what gives result:
[<re.Match object; span=(1, 4), match='abc'>, <re.Match object; span=(180, 183), match='abc'>]
<callable_iterator object at 0x00000116F35D5390>
!! an iteration is missing in the result !!