[SOLVED] removing a particular pattern from a given string in python

Issue

i would like generate the following output from the string "[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"

expected output

foo foo foo \r\n\r\n\r\n

Solution

So – remove all [cid:...] blocks and any newlines/carriage-returns trailing them?

>>> import re
>>> s = "[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
>>> re.sub(r"\[cid:(.+?)\][\r\n]*", "", s)
'foo foo foo \r\n\r\n\r\n'

Answered By – AKX

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *