且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在python中查找并删除以特定子字符串开头和结尾的字符串

更新时间:2023-01-22 19:29:26

您可以使用 re.sub :

>>>s='dasdasdsafs[图像:图像名称:图像]vvfd gvdfvg dfvgd'>>>re.sub(r'\[image.+image\]','',s)'dasdasdsafsvvfd gvdfvg dfvgd'

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.

You can use re.sub :

>>> s='dasdasdsafs[image : image name : image]vvfd gvdfvg dfvgd'
>>> re.sub(r'\[image.+image\]','',s)
'dasdasdsafsvvfd gvdfvg dfvgd'