String Replace only if whole word?
m.yanowitz at kearfott.com
Fri Nov 17 13:54:05 CET 2006
I am hoping someone knows if there is an easier way to do this or someone
already implemented something that does this, rather than reinventing the
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:
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:
It would replace the string with:
Is there any easy way to avoid this, only replace if a whole word
I probably need something which determines when a word ends, and I will
a word as containing only 'A'-'Z','a'-'z','0'-'9','_' . As long as the
contains more of the word digits after the match, don't replace?
Thanks in advance:
More information about the Python-list