I’m having trouble finding the correct regular expression for the scenario below:
Lets say:
a = "this is a sample"
I want to match whole word – for example match "hi" should return False since "hi" is not a word and "is" should return True since there is no alpha character on the left and on the right side.
Advertisement
Answer
Try
re.search(r'bisb', your_string)
From the docs:
bMatches the empty string, but only at the beginning or end of a word.
Note that the re module uses a naive definition of “word” as a “sequence of alphanumeric or underscore characters”, where “alphanumeric” depends on locale or unicode options.
Also note that without the raw string prefix, b is seen as “backspace” instead of regex word boundary.
