By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,386 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,386 IT Pros & Developers. It's quick & easy.

How to match word boundary?

P: n/a
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
Jul 22 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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>

Jul 22 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.