How to match word boundary?

Fredrik Lundh fredrik at pythonware.com
Tue Jul 22 18:45:36 EDT 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