How to match word boundary?

Discussion in 'Python' started by Peng Yu, Jul 22, 2008.

  1. Peng Yu

    Peng Yu Guest

    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
    Peng Yu, Jul 22, 2008
    #1
    1. Advertising

  2. 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>
    Fredrik Lundh, Jul 22, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yves Tchapda

    Boundary scan clocking

    Yves Tchapda, Sep 17, 2003, in forum: VHDL
    Replies:
    0
    Views:
    521
    Yves Tchapda
    Sep 17, 2003
  2. Singleton

    what is word boundary?

    Singleton, Sep 18, 2005, in forum: C++
    Replies:
    14
    Views:
    668
    Andrew Koenig
    Sep 19, 2005
  3. arun
    Replies:
    2
    Views:
    104
  4. PerlFAQ Server
    Replies:
    0
    Views:
    105
    PerlFAQ Server
    Jan 31, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    221
    PerlFAQ Server
    Apr 24, 2011
Loading...

Share This Page