Re: String Replace only if whole word?

Discussion in 'Python' started by Tim Chase, Nov 17, 2006.

  1. Tim Chase

    Tim Chase Guest

    > I have been using the string.replace(from_string, to_string, len(string))
    > to replace names in a file with their IP address.
    > For example, I have definitions file, that looks something like:
    > 10.1.3.4 LANDING_GEAR
    > 20.11.222.4 ALTIMETER_100
    > 172.18.50.138 SIB
    > 172.18.50.138 LAPTOP
    > 172.18.51.32 WIN2000
    > 127.0.0.1 LOCALHOST
    >
    > and I have a text file (a Python script) that has these names in the file.
    > In most cases the string.replace() command works great. But there is one
    > instance which it fails:
    > Suppose I had in the file:
    > if (LAPTOP_IS_UP()):
    > It would replace the string with:
    > if ("172.18.50.138"_IS_UP()):
    >
    > Is there any easy way to avoid this, only replace if a whole
    > word matches? I probably need something which determines when
    > a word ends, and I will define a word as containing only
    > 'A'-'Z','a'-'z','0'-'9','_' . As long as the string contains
    > more of the word digits after the match, don't replace?


    A solution I've used in the past (whether it's good or not, I'd
    be interested to get feedback from the list)

    >>> mapping = {}
    >>> for line in file('definitions.txt'):

    .... ip, name = line.split()
    .... mapping[name] = '"%s"' % ip
    ....
    >>> import re
    >>> s = "LAPTOP LAPTOP_IS_UP"
    >>> r = re.compile(r'\b\w+\b')
    >>> r.sub(lambda match: mapping.get(match.group(0),

    match.group(0)), s)
    '"172.18.50.138" LAPTOP_IS_UP'


    The regexp '\b\w+\b' finds "word"s (where \w is locale specific,
    but could easily be rewritten as '\b[a-zA-Z0-9_]+\b' if you
    needed) while the '\b' portion ensures that there's a
    word-to-non-word boundary coming or going.

    It then goes through and replaces every word in the string with
    either the result of looking it up in the magic mapping, or with
    its original contents.

    -tkc
     
    Tim Chase, 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:
    912
    \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:
    363
    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:
    580
    Carsten Haese
    Nov 17, 2006
  4. Michael Yanowitz

    RE: String Replace only if whole word?

    Michael Yanowitz, Nov 17, 2006, in forum: Python
    Replies:
    1
    Views:
    777
    Leonhard Vogt
    Nov 19, 2006
  5. perl Newbie

    Match Whole word only

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

Share This Page