I have the following string
a = "animal: [cat.], animal:[dog.]"
print(a)
>>> animal: [cat.], animal:[dog.]
I would like to replace each part of the string between [
and .]
(included) with a given value (e.g. frog
).
The expected output is:
animal: frog, animal: frog
I tried the following so far:
import re
b = re.sub(r'(\[\b).*(\b\.])','frog', a)
print(b)
>>> animal: frog
That it slightly different from the expected output.
I think this is due to the fact the code see the first [
and the last .]
as delimiters, consequently replacing with frog
all the string between.
Instead, I would like the code to consider two pairs of delimiters: those containing the word cat
and those containing the word dog
.
Do you have any suggestions?