Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to match whole word

Reply
Thread Tools

how to match whole word

 
 
Peng Yu
Guest
Posts: n/a
 
      07-16-2008
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)
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      07-16-2008
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


 
Reply With Quote
 
 
 
 
Peng Yu
Guest
Posts: n/a
 
      07-16-2008
On Jul 15, 10:29 pm, Gary Herron <(E-Mail Removed)> 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      07-16-2008
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>

 
Reply With Quote
 
John S
Guest
Posts: n/a
 
      07-16-2008
On Jul 16, 9:38 am, Peng Yu <(E-Mail Removed)> wrote:
> On Jul 15, 10:29 pm, Gary Herron <(E-Mail Removed)> 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


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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      07-16-2008
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Match Whole word only perl Newbie Perl Misc 7 04-10-2009 08:40 PM
String Replace only if whole word? Michael Yanowitz Python 1 11-17-2006 01:38 PM
*WITHOUT* using: ValidateRequest="False" for the whole page (or my whole site).... \A_Michigan_User\ ASP .Net 2 08-21-2006 02:13 PM
Word - setting a tab for the whole document Jim Computer Support 4 07-01-2003 10:14 AM



Advertisments