I am reading regex pattern from a .txt file and passing it as a variable and using it to search in a very big text file. However, variable passing in regex search didnt work. My code snippet is
with open(r"C:\Desktop\list_pattern.txt", "r") as file1:
for pattern in file1:
with open(r'C:\Desktop\log.txt',"r") as my_file:
for lines in my_file:
k=re.search('{}'.format(pattern), lines) # I even tried re.search(pattern, lines)
if k!=None:
print("k is",k)
For example, the first lne in list_pattern.txt is "Battery Low" and it gives 0 match in log.txt. However, if i replace the code line with k=re.search('Battery Low', lines), it gives 12 match. Any idea what may be wrong? I am using python 3.10.