How to match word boundary?

Fredrik Lundh fredrik at pythonware.com
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>
 >>> _.group()
'a::'
 >>> re.match("\w::(?=\w)", "a:: b")
 >>>

</F>




More information about the Python-list mailing list