Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and remove a string starting and ending with a specific substring in python

I have a string similar to "dasdasdsafs[image : image name : image]vvfd gvdfvg dfvgd". From this string, I want to remove the part which stars from [image : and ends at : image] . I tried to find the 'sub-string' using following code-

result = re.search('%s(.*)%s' % (start, end), st).group(1)

but it doesn't give me the required result. Help me to find the correct way to remove the sub-string from the string.

like image 953
n.imp Avatar asked Aug 14 '15 17:08

n.imp


1 Answers

You can use re.sub :

>>> s='dasdasdsafs[image : image name : image]vvfd gvdfvg dfvgd'
>>> re.sub(r'\[image.+image\]','',s)
'dasdasdsafsvvfd gvdfvg dfvgd'
like image 98
Mazdak Avatar answered Nov 14 '22 21:11

Mazdak