By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,596 Members | 3,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,596 IT Pros & Developers. It's quick & easy.

String Replace only if whole word?

P: n/a
Hello:

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
wheel:
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?

Thanks in advance:
Michael Yanowitz
Nov 17 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Michael Yanowitz wrote:
Hello:

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
wheel:
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?

Thanks in advance:
Michael Yanowitz
You need regular expressions for this. Use the re module.
http://docs.python.org/lib/module-re.html

from the docs:

re.sub(pattern, repl, string[, count])
Return the string obtained by replacing the leftmost non-overlapping
occurrences of pattern in string by the replacement repl.

Your pattern would be "[^A-Za-z0-9_]word[^A-Za-z0-9_]"

[^xy] is approximately not in ('x', 'y')

--
Juho Schultz

Nov 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.