(Sep-19-2020, 10:36 AM)Secret Wrote: 1) do I have to place my variable (text_line) in quotation marks here?Now is just a string
'text_lines'
,then this string is all that you search in readlines()
give back a list
,and you can not pass a list to a regex search.Have to loop over
readlines()
or ''.join()
to string.text_lines = f.readlines() for line in text_lines: # Do regex stuff with lineWhen loop over many lines in regex can you re.finditer() for only load line bye line in memory,and avoid load the whole list.
Example.
import re data = '''\ hi model choice : 999 model choice : abc The green car had a model choice : Red color''' pattern = re.compile(r"(?<=model choice :).*") for match in pattern.finditer(data): print(match.group().strip())
Output:999
abc
Red color