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)
)
split()to split the string into a list of words. Then replace in the list. Finally usejoin()to convert it back to a string.