I'm getting false positives in python for the following example. I'm trying to find if a key word exist in a string. The problem is that the string has words connected by usually an underscore or hyphen so I only want positive result if the keyword exist when not in a word. It can be surround by hyphen,underscore or anything that is not a letter to be consider True result. Typically it should be surrounded by underscore or hyphen. It is not case sensitive as well.
test_list = ['server_test', 'server_dev', 'server_uat', 'server_dr', 'server-dr-NA', 'server-DR', 'dress_prod', 'testosterone','uatae','devacurl', 'dev_server']
The result should output this list of True/False
[True, True, True, True, True, True, False, False, False, False, True]
Implementation:
key_words = ['uat','dr','test','qa','dev']
for name in test_list:
if any(x in name.lower() for x in key_words):
print('True')
else:
print('False')
Results:
True
True
True
True
True
True
True
True
True
True
Is there better way of doing this in python?
If not how would I do this using regex in python?
Please keep in mind this is being looped over a large data set where performance does matter.