how to match whole word

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

  1. Peng Yu

    Peng Yu Guest

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

  2. Peng Yu

    Gary Herron Guest

    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
    Gary Herron, Jul 16, 2008
    #2
    1. Advertising

  3. Peng Yu

    Peng Yu Guest

    On Jul 15, 10:29 pm, Gary Herron <> 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
    Peng Yu, Jul 16, 2008
    #3
  4. 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>
    Fredrik Lundh, Jul 16, 2008
    #4
  5. Peng Yu

    John S Guest

    On Jul 16, 9:38 am, Peng Yu <> wrote:
    > On Jul 15, 10:29 pm, Gary Herron <> 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
    John S, Jul 16, 2008
    #5
  6. 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>
    Fredrik Lundh, Jul 16, 2008
    #6
    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. \A_Michigan_User\
    Replies:
    2
    Views:
    873
    \A_Michigan_User\
    Aug 21, 2006
  2. Michael Yanowitz

    String Replace only if whole word?

    Michael Yanowitz, Nov 17, 2006, in forum: Python
    Replies:
    1
    Views:
    357
    Juho Schultz
    Nov 17, 2006
  3. Carsten Haese

    RE: String Replace only if whole word?

    Carsten Haese, Nov 17, 2006, in forum: Python
    Replies:
    0
    Views:
    568
    Carsten Haese
    Nov 17, 2006
  4. Tim Chase
    Replies:
    0
    Views:
    413
    Tim Chase
    Nov 17, 2006
  5. perl Newbie

    Match Whole word only

    perl Newbie, Apr 10, 2009, in forum: Perl Misc
    Replies:
    7
    Views:
    196
Loading...

Share This Page