how to match whole word

Peng Yu PengYu.UT at gmail.com
Wed Jul 16 12:38:26 EDT 2008


On Jul 15, 10:29 pm, Gary Herron <gher... at islandtraining.com> wrote:
> 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



More information about the Python-list mailing list