I am trying to search for exact words in a file. I read the file by lines and loop through the lines to find the exact words. As the in
keyword is not suitable for finding exact words, I am using a regex pattern.
def findWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
The problem with this function is that is doesn't recognizes square brackets [xyz]
.
For example
findWord('data_var_cod[0]')('Cod_Byte1 = DATA_VAR_COD[0]')
returns None
whereas
findWord('data_var_cod')('Cod_Byte1 = DATA_VAR_COD')
returns <_sre.SRE_Match object at 0x0000000015622288>
Can anybody please help me to tweak the regex pattern?