I was trying to find a whole word using regex
in Python 3. The best I can come up with is:
import re
search_string = 'CIS 22B : Intermediate Programming Methodologies in C++'
user_input = 'C++'
pattern = r'\b'+re.escape(user_input)
print(re.search(pattern,search_string))
Though, it has a flaw.. search would still be a success if this search string had the word C++xyz
instead of C++
. Which I don't want, I want it to match the word exactly.
There are answers for this problem on StackOverflow.. but none of them worked for me. Probably because they were for older version of Python.
Also, even for this code the method search()
resulted an output.. but match()
didn't. I have no idea why.
Please help!