It is known that \b
means word boundary in regular expression. However the following code of re
module in python doesn't work:
>>> p=re.compile('\baaa\b')
>>> p.findall("aaa vvv")
[]
I think the returned results of findall
should be ["aaa"]
, however it didn't find anything. What's the matter?
You need to use a raw string, or else the \b
is interpreted as a string escape. Use r'\baaa\b'
. (Alternatively, you can write '\\b'
, but that is much more awkward for longer regexes.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With