How to match word boundary?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peng Yu

    How to match word boundary?

    Hi,

    I use r"\ba\b" to match "a". However, I can not use "\ba::\b" to match
    "a::b".

    I would like to match "a::" in "a::b", but not in "a:: b". That is,
    the character after "::" should be a alphanumeric character. Could you
    let me know how to do it and why "\b" would not work?

    Thanks,
    Peng
  • Fredrik Lundh

    #2
    Re: How to match word boundary?

    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_Mat ch object at 0x01442138>
    >>_.group()
    'a::'
    >>re.match("\w: :(?=\w)", "a:: b")
    >>>
    </F>

    Comment

    Working...