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

Replace multiline string using python regex?

If I remove the \n after the b I get the string REPLACED. However I need to replace newlines and I thought M and S flag would let dot match and replace a multiline string but it appears not to

re.sub(r"aA.*Z", "REPLACED", "aAb\nZ\n//ok", re.M|re.S)

The real data I’d like to replace is "^void c_like_func_NAME(){…}". I believe I can figure out the rest once I can replace across lines

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 :

Your logic is correct, but it appears that you need to explicitly specify the flags field in the 4th parameter. Hence, the following works:

print(re.sub(r"aA.*Z", "REPLACED", "aAb\nZ\n//ok", flags=re.M|re.S))

This prints:

REPLACED
//ok
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