Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

removing a particular pattern from a given string in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading