0

I'm opening a file and reading the file line by line and attempting to use re.findall to search each line for an exact match to an item in a list, but for each line in the file, I only want to match if it's an exact match (meaning nothing connected to the word) and at the start of the line with no leading white space(s) or tab(s). I'm close I think, but when there is leading spaces and/or a match of connected words it is returning a match. Here's my example code with 4 examples with #1 not working:

lst_animals = ['dogs', 'cats', 'deer', 'squirrels', 'birds']

lst_animals1 = re.findall(r'^[(a-z_)]*' + '|'.join(lst_animals), '   "testing_spaces_animal_deer" = {') #leading space(s) - DO NOT want to match in this case (NOT working)
lst_animals2 = re.findall(r'^[(a-z_)]*' + '|'.join(lst_animals), 'deer = {') #Start of line, NO space(s) or tab(s) - want to match (working)
lst_animals3 = re.findall(r'^[(a-z_)]*' + '|'.join(lst_animals), '\t"testing_tab_animal_dogs" = {') #leading tab(s) - DO NOT want to match (working)
lst_animals4 = re.findall(r'^[(a-z_)]*' + '|'.join(lst_animals), 'dogs = {') #Start of line, NO space(s) or tab(s) - want to match (working)

print(lst_animals1)
print(lst_animals2)
print(lst_animals3)
print(lst_animals4)```  

Any ideas?  Thanks in advance!
cd2
  • 28
  • 3

0 Answers0