import re
if re.search(r"\b3\ \b", 'SGM export 3 honey 400gr box new'):
print('ok')
else:
print('failed')
# failed
CodePudding user response:
The word break, \b
, matches the boundary between word characters and non-word characters. The issue you are running into is that the plus character, ' '
is not a word character, but neither is the following space.
So the regex patter does not match, because there is no word/non-word boundary. To account for this, you can add a possible white space character after the plus.
import re
if re.search(r"\b3\ \s?\b", 'SGM export 3 honey 400gr box new'):
print('ok')
else:
print('failed')
CodePudding user response:
Try without last \b:
import re
if re.search(r"\b3\ ", 'SGM export 3 honey 400gr box new'):
print('ok')
else:
print('failed')
#ok
----------EDIT-------------
Here's definition what \b means and where does it should be. Summary - \b is a backspace, and currently here - \b3\ \b
regex can't find
because for it formula look like \b3
and nothing more.