Reputation: 93
I'm trying to make a function to remove whole words from a string in python, and I think I have something that does it:
def remove_words_from_str(strn, word, replacement=' '):
return re.sub(r'(\s*)'+word+'(\s*)', replacement, strn)
The problem is this takes pieces of words too, which I don't want.
EX: print( remove_words_from_str( "is this is a test ? yes this is ; this is", "is" ) )
OUT: th a test ? yes th ; th
Is there a way to only take whole words? (In other words, I don't want 'this' to go to 'th', cause the 'is' in 'this' is not a full word)
Upvotes: 1
Views: 1155
Reputation: 863
solution without using a regex:
def remove_words_from_str(strn, word, replacement=' '):
return " ".join([replacement if token==word else token for token in strn.split()])
Upvotes: 0
Reputation: 33
You could use the .split()
method on the list to break it down into single words (splits at blanks if no argument is given). And then simply go with
list.remove(elem)
Upvotes: 0
Reputation: 114230
Python regex supports a \b
symbol, which means "word" boundary. So you can do
re.sub(r'\s*\b' + word + r'\b\s*', replacement, strn)
You will still want to keep the greedy \s*
quantifiers on both sides to replace all the surrounding spaces with a single space.
The output for your test case is
' this a test ? yes this ; this '
If you want to ensure that the first and last space are removed, use str.strip
on the result:
def remove_words_from_str(strn, word, replacement=' '):
return re.sub(r'\s*\b' + word + r'\b\s*', replacement, strn).strip()
Upvotes: 4
Reputation: 339
This worked for me.
def remove_words_from_str(strn, word, replacement=' '):
return re.sub(r'(^|\s+)'+word+'($|\s+)', replacement, strn)
Upvotes: 1