How to simplify following regular expression
re.search(f'\W{word}\W', text) or re.search(f'^{word}\W', text) or re.search(f'\W{word}$', text) or word == text
i.e. return True for any string that contains word with \W or ^ before and \W or $ after.
Variants
re.search(f'[^\W]{word}[\W$]', text)
re.search(f'[\W^]{word}[\W$]', text)
dont work for my case.
Expression re.search(f'\W*{word}\W*', text)
gives wrong matches, for example word + 'A'
.
Any suggestions? Thank you!