RE: String Replace only if whole word?

Discussion in 'Python' started by Carsten Haese, Nov 17, 2006.

  1. On Fri, 2006-11-17 at 10:43 -0500, Michael Yanowitz wrote:
    > >Your pattern would be "[^A-Za-z0-9_]word[^A-Za-z0-9_]"
    > >
    > >--
    > >Juho Schultz

    >
    >
    > Thanks.
    > This works great except for one thing:
    >
    > The character after the replacement is deleted, so that if I have
    > send_data (LAPTOP, test_string)
    > it would replace it with:
    > send_data (10.1.3.4 test_string)
    > (ignoring that the 10.1.3.4 is not quoted). The comma is missing.


    If you actually wanted to use string replacement, you'd need a pattern
    with look-behind and look-ahead assertions along the lines of
    "(?<![A-Za-z0-9_])word(?![A-Za-z0-9_])".

    Then again, I don't think string replacement is the right tool for the
    job. You're saying that the file you are operating on in this fashion is
    a Python script. Why don't you just place something like
    LAPTOP="10.1.3.4" at the top of that file, or have the script read this
    information from a configuration file?

    -Carsten
    Carsten Haese, Nov 17, 2006
    #1
    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. Tim Chase
    Replies:
    0
    Views:
    413
    Tim Chase
    Nov 17, 2006
  4. Michael Yanowitz

    RE: String Replace only if whole word?

    Michael Yanowitz, Nov 17, 2006, in forum: Python
    Replies:
    1
    Views:
    766
    Leonhard Vogt
    Nov 19, 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