0

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)
)
3
  • Use split() to split the string into a list of words. Then replace in the list. Finally use join() to convert it back to a string. Commented Mar 23, 2022 at 21:35
  • 1
    What has a regex ever done to hurt you? You have something that works, but do not want it. Is this a school project? Commented Mar 23, 2022 at 21:36
  • great idea @Barmar I will do this post the answer if you want and I'll accept! Commented Mar 23, 2022 at 21:36

3 Answers 3

1

There are a couple of great and simpler answers here already that convert haystack to a list, perform the replacement and then convert it back to a string. If you still want to use haystack as a string throughout, check out this solution I made.

def replace_all_whole_words2(needle, replacement, haystack):
    # Counter to go through characters in haystack
    i = 0 
    
    # This will collect all chars separated by space as word
    word = '' 
    
    # len_needle will control replacement insertion and diff will adjust i after insertion 
    len_needle = len(needle)
    diff = len(replacement) - len_needle
    
    # Go through characters in haystack and replace needle with replacement
    while i < len(haystack):
        char = haystack[i]
        if char == ' ':
            if word == needle:
                haystack = haystack[:(i-len_needle)] + replacement + haystack[i:]
                
                # Adjust i so that you can continue from the next char after the space
                i += diff
                
            # Reset word to collect new chars
            word = ''
        else:
            word += char
        
        i += 1
        
    return haystack

Test:

needle = "test"
replacement = "replacement"
haystack = "test test test testa atest"

replace_all_whole_words2(needle, replacement, haystack)

Output:

'replacement replacement replacement testa atest'
Sign up to request clarification or add additional context in comments.

1 Comment

good stuff, thanks for posting an answer relative to my function cool to see. also thanks for comments.
1

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)

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.