Python Search and Replace Using a List for Search

Viewed 53

I have several lines of a file that I'm looping through and have stored as strings and I'm looking to perform a simple search and replace in each line using either the method built into python strings str.replace() or using regular expressions re.sub() but using a list as argument for the old substring. I know the format usually goes as follows:

string.replace('oldsubstring','newsubstring')

However, if I have a list of strings: ['word1', 'word2', 'word3'], is it possible to use this as the oldsubstring argument so that if any of the elements in the list are found in string, that element is replaced with newsubstring. I know this is possible using a double nested for loop that loops through all of my lines and my list of strings, but I'm looking for a more efficient algorithm to accomplish this.

Follow Up Question:

Another problem I have found is that there are times where my list of strings will look like:

['word1', 'word1_suffix', 'word2', 'word3'] NOTE: Order of these elements is not guaranteed to be the same each run.

When using the double nested for loop method, if word1_suffix appears in the current line I'm looking at, and I then loop through my list of strings, if word1 happens to appear in my list of strings first, the replacement will be newsubstring_suffix rather than replacing the entire substring: word1_suffix with newsubstring.

NOTE: I know that using a regular expression I can ensure that word1_suffix is it's own full word surrounded by spaces, but that are times where I do want a part of my line that follows the format: word1_miscellaneous to be replaced as newsubstring_miscellaneous so that method will not entirely solve my problem.

1 Answers

With re.sub you can use the greedy character of regex to make sure word1_suffix isn't replaced by newsubstring_suffix:

your_string = "hello word1_suffix world word3"

word_list = ['word1', 'word1_suffix', 'word2', 'word3']
word_set = set(word_list)

# pattern to match all 'words' (succession of letters, digits and _):
word_pattern = re.compile(r'\w+')
print(re.sub(word_pattern, lambda x: "newsubstring" if x.group() in word_set else x.group(), your_string))

The lambda function check if the matched group is in word_set and replace it with newsubstring.

Output:

hello newsubstring world newsubstring
Related