How to match word boundary?

Fredrik Lundh fredrik at
Wed Jul 23 00:45:36 CEST 2008

Peng Yu wrote:

> I would like to match "a::" in "a::b", but not in "a:: b". That is,
> the character after "::" should be a alphanumeric character.

sounds like a look-ahead assertion is what you need:

 >>> import re
 >>> re.match("\w::(?=\w)", "a::b")
<_sre.SRE_Match object at 0x01442138>
 >>> re.match("\w::(?=\w)", "a:: b")


More information about the Python-list mailing list