Replace a substring when it is a single word

I am trying to replace the line, that is, “H3” in the file with “H1”, but I want to replace “H3” and not “mmmoleculeH3” to become “mmmoleculeH1”. I tried, but my limited knowledge in python didn't deliver me anywhere. If there is any other method different from what would be great. The script I'm using now is:

#!/usr/bin/python import fileinput import sys def replaceAll(file,searchExp,replaceExp): for line in fileinput.input(file, inplace=1): if searchExp in line: line = line.replace(searchExp,replaceExp) sys.stdout.write(line) replaceAll("boxFile.cof","H3","H1") 

If there is any way I can do it with this myself, without using re, then that would be great. Thanks in advance.

+4
source share
2 answers

As others have said, this is the case when regular expressions are the right tool.

You can replace only whole words using \b :

 >>> text = 'H3 foo barH3 H3baz H3 quH3ux' >>> re.sub(r'\bH3\b', 'H1', text) 'H1 foo barH3 H3baz H1 quH3ux' 
+6
source

Since I was curious to do this without regular expression, here is the version without:

 MYSTR = ["H3", "H3b", "aH3", "H3 mmmoleculeH3 H3", "H3 mmmoleculeH3 H3b", "H3 mmmoleculeH3 H3b H3"] FIND = "H3" LEN_FIND = len( FIND ) REPLACE = "H1" for entry in MYSTR: index = 0 foundat = [] # Get all positions where FIND is found while index < len( entry ): index = entry.find( FIND, index ) if index == -1: break foundat.append( index ) index += LEN_FIND print "IN: ", entry, for loc in foundat: # Check if String is starting with FIND if loc == 0: # Check if String only contains FIND if LEN_FIND == len( entry ): entry = REPLACE # Check if the cahracter after FIND is blank elif entry[LEN_FIND] == " ": entry = entry[:loc] + REPLACE + entry[loc + LEN_FIND:] else: # Check if character before FIND is blank if entry[loc - 1] == " ": # Check if FIND is the last part of the string if loc + LEN_FIND + 1 > len( entry ): entry = entry[:loc] + REPLACE + entry[loc + LEN_FIND:] # Check if character after FIND is blank elif entry[loc + LEN_FIND] == " ": entry = entry[:loc] + REPLACE + entry[loc + LEN_FIND:] print " OUT: ", entry 

Output:

 IN: H3 OUT: H1 IN: H3b OUT: H3b IN: aH3 OUT: aH3 IN: H3 mmmoleculeH3 H3 OUT: H1 mmmoleculeH3 H1 IN: H3 mmmoleculeH3 H3b OUT: H1 mmmoleculeH3 H3b IN: H3 mmmoleculeH3 H3b H3 OUT: H1 mmmoleculeH3 H3b H1 

PS: I would prefer a solution from Daniel Roseman.

+3
source

Source: https://habr.com/ru/post/1502036/


All Articles