473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String Replace only if whole word?

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
1 9006
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: - ions | last post by:
Hi, i would like to know how to replace every char in a string with a certin given char using the String.replace(char oldChar,char newChar). I would like to replace all letters with an underscore...
5
by: Phrederik | last post by:
Hey all! New to javascript and still getting my head around strings... Consider the following line of code... var path = location.pathname; ....after execution, the variable "path"...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
1
by: James Vitale | last post by:
Using vb asp.net 1.1 I'm doing a word automation on a doc file and trying to do a find and replace. My existing code works fine except that it doesn't find and replace in the header. My code...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
10
by: pantagruel | last post by:
Hi, I'm looking for an optimal javascript function to split a camelcase string and return an array. I suppose one could loop through the string, check if character is uppercase and start...
4
by: apsonline | last post by:
hi m trying to write a code that would find an abusive word from a given a string and then compare this word with the list of abusive words, maintained in an array char abusive, and replace them...
10
by: Antoine De Groote | last post by:
Hi there, I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.