I am wanting to replace whole words with another string without using regex. The replace_all_whole_words1
does what I want, but I don't want to use regex and is only being used to test if my replace_all_whole_words2
function is working correctly (it isn't).
import re
def replace_all_whole_words1(needle, replacement, haystack):
needle = re.escape(needle)
return re.sub(r"\b%s\b" % needle, replacement, haystack)
def replace_all_whole_words2(needle, replacement, haystack):
i=0
while len(haystack) > i:
found_word = False
found_word_start = i
found_word_end = 0
found_type = 0 # will repersent if we found, word " " or " " word or " " word " " ()
if i == 0:
found_word = haystack[i:i len(needle " " )] == needle " "
found_word_end = i len(needle)
found_type = 1
elif i == len(haystack) - len(" " needle):
found_word = haystack[i:i len(" " needle)] == " " needle
found_word_end = i len(" " needle)
found_type = 2
else:
found_word = haystack[i:i len(" " needle " " )] == " " needle " "
found_word_end = i len(" " needle " ")
found_type = 3
if found_word:
print(haystack, found_word_start, found_word_end, i, found_type)
haystack = haystack[:found_word_start] replacement haystack[found_word_end:]
i = 1
return haystack
needle = "test"
replacement = "replaced"
haystack = "test test test testa atest"
print(
replace_all_whole_words1(needle, replacement, haystack) == replace_all_whole_words2(needle, replacement, haystack)
)
print(
replace_all_whole_words2(needle, replacement, haystack)
)
CodePudding user response:
Why not convert to list? Since you are just deleting words.
def replace_all_whole_words2(needle, replacement, haystack):
l = haystack.split()
for idx, word in enumerate(l):
if word == needle:
l[idx] = replacement
return ' '.join(l)
CodePudding user response:
As explained in the comments, separating with split, using list comprehension and then re joining:
def replace_all_whole_words2(needle, replacement, haystack):
word_list = haystack.split(' ')
new_list = [replacement if word == needle else word for
word in word_list]
return " ".join(new_list)