Reputation: 663
I have a list of words list_words =["cat","dog","animals"]
And i have a text = "I have a lot of animals a cat and a dog"
I want a regex code that is able to add a comma at the end of every word before any word in the list given.
I want my text to be like that: text = "I have a lot of, animals a, cat and a, dog"
My code so far:
import re
list_words = ["cat", "dog", "animals","adam"]
text = "I have a lot of animals, cat and a dog"
for word in list_words:
if word in text:
word = re.search(r" (\s*{})".format(word), text).group(1)
text = text.replace(f" {word}", f", {word}")
print(text)
But i have 2 issues here:
1: if i have a text like this : text= I have a lot of animals cat and a dogy
it turns it into : text= I have a lot of, animals, cat and a, dogy
which is not the result wanted, i wanted to replace only the word itself not with
addition like dogy
2: if i have a text like this: text= I have a lot of animals, cat and a dogy
it still add another comma which is not what i want
Upvotes: 2
Views: 2394
Reputation: 370
All words get a comma:
import re
list_words = ["cat", "dog", "animals"]
text = "I have a lot of animals a cat and a dog"
for word in list_words:
word = re.search(r" (\s*{}) ".format(word), text)
text = text.replace(f" {word}", f", {word}")
Upvotes: 1
Reputation: 627488
You can use
,*(\s*\b(?:cat|dog|animals|adam))\b
See the regex demo. Details:
,*
- zero or more commas(\s*\b(?:cat|dog|animals|adam))
- Group 1:
\s*
- zero or more whitespaces\b
- a word boundary(?:cat|dog|animals|adam)
- one of the words\b
- word boundarySee the Python demo:
import re
list_words = ["cat", "dog", "animals", "adam"]
text = "I have a lot of animals, cat and a dog"
pattern = r",*(\s*\b(?:{}))\b".format("|".join(list_words))
print( re.sub(pattern, r",\1", text) )
# => I have a lot of, animals, cat and a, dog
Upvotes: 2
Reputation:
Go with a simpler method.
list_words =["cat","dog","animals"]
text = "I have a lot of animals a cat and a dog"
test_list_words=[]
for new in text.split(" "):
if new in list_words:
new=new+","
test_list_words.append(new)
else:
test_list_words.append(new)
print(' '.join(test_list_words))
Upvotes: 0