Hy, I have an issue with regex python 3.
Here is my code :
# Here is regex 1, have to find tk_common on file tmp/out-file.txt
regex1 = re.compile(r'tk_common')
with open("tmp/out-file.txt") as f:
for line in f:
result = regex1.search(line)
# If found :
if regex1.search is not None:
print("I find tk_common in out-file")
# else :
else:
print("I didn't find tk_common in out-file")
# And here is the second one
regex2 = re.compile(r'tk_text')
with open("tmp/out-file.txt") as f:
for line in f:
result = regex2.search(line)
if regex2.search is not None:
print("I find tk_text in out-file")
else:
print("I didn't fint tk_text in out-file")
My issue :
I have two print message success when i start my programm :
I find tk_common in out-file
I find tk_text in out-file
But in fact, it should not :
$ cat tmp/out-file.txt | grep "tk_common\|tk_text"
<div class="tk_common">
Any idea ? Thanks,
None). You are looking forresultwhich is the result. Besides your code will only check the last line, because go to everyline and after you checked each line you investigate the result.