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

how to match whole word

P: n/a
Hi,

The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?

Thanks,
Peng

if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
Jul 16 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Peng Yu wrote:
Hi,

The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?

Thanks,
Peng

if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
--
http://mail.python.org/mailman/listinfo/python-list
The regular expression "\w+" will match (what might be your definition
of) a word, and in particular will match abc in :abc:. Regular
expressions have lots of other special \-sequences that might be worth
your while to read about: http://docs.python.org/lib/re-syntax.html

Gary Herron
Jul 16 '08 #2

P: n/a
On Jul 15, 10:29 pm, Gary Herron <gher...@islandtraining.comwrote:
Peng Yu wrote:
Hi,
The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?
Thanks,
Peng
if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
--
http://mail.python.org/mailman/listinfo/python-list

The regular expression "\w+" will match (what might be your definition
of) a word, and in particular will match abc in :abc:. Regular
expressions have lots of other special \-sequences that might be worth
your while to read about: http://docs.python.org/lib/re-syntax.html

Gary Herron
I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.

Thanks,
Peng
Jul 16 '08 #3

P: n/a
Peng Yu wrote:
I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.
as the documentation says, \A and \Z matches at the beginning/end of a
*string*, not a word.

</F>

Jul 16 '08 #4

P: n/a
On Jul 16, 9:38 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 15, 10:29 pm, Gary Herron <gher...@islandtraining.comwrote:
Peng Yu wrote:
Hi,
The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?
Thanks,
Peng
if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
--
>http://mail.python.org/mailman/listinfo/python-list
The regular expression "\w+" will match (what might be your definition
of) a word, and in particular will match abc in :abc:. Regular
expressions have lots of other special \-sequences that might be worth
your while to read about: http://docs.python.org/lib/re-syntax.html
Gary Herron

I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.

Thanks,
Peng
Not sure why you picked \A and \Z -- they are only useful if you are
using the re.M flag.
What you want is \b -- match word boundary, on either side of your
word:

regex = re.compile(r"\b" + re.escape(old_str) + r"\b",re.I)

re.I is the same as re.IGNORECASE. More than one option may be OR'ed
together. There's no such thing as "re.O" in Python. I can understand
where you get the idea, as there is an 'o' modifier for REs in Perl.

To summarize, \A and \Z match the beginning and end of a STRING, while
\b matches the beginning or end of a WORD.

-- john
Jul 16 '08 #5

P: n/a
John S wrote:
Not sure why you picked \A and \Z -- they are only useful if you are
using the re.M flag.
Well, they're aliases for ^ and $ in "normal" mode, at least for strings
that don't end with a newline.
re.I is the same as re.IGNORECASE. More than one option may be OR'ed
together. There's no such thing as "re.O" in Python. I can understand
where you get the idea, as there is an 'o' modifier for REs in Perl.
His code did

opts.ignore_case and re.I or 0

which is the same as "re.I if opts.ignore_case else 0" in Python 2.5,
where 0 is a zero and not an O.

</F>

Jul 16 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.