Ad

Why This Regex \b3\+\b Can't Match "3+"?

- 1 answer

import re
if re.search(r"\b3\+\b", 'SGM export 3+ honey 400gr box new'):
    print('ok')
else:
    print('failed')
# failed
Ad

Answer

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')
Ad
source: stackoverflow.com
Ad