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

Regex to Remove Block Comments but Keep Empty Lines?

Is it possible to remove a block comment without removing the line breaks with a regex?

Let’s say I have this text:

text = """Keep this /* this has to go
this should go too but leave empty line */
This stays on line number 3"""

I came up with this regex:

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

text = re.sub(r'/\*.*?\*/', '', text, 0, re.DOTALL)

But this gives me:

Keep this 
This stays on line number 3

What I want is:

Keep this

This stays on line number 3

Can it be done?

>Solution :

We can make a slight change to your current logic and use a lambda callback as the replacement for re.sub:

import re

text = """Keep this /* this has to go
this should go too but leave empty line */
This stays on line number 3"""

text = re.sub(r'/\*.*?\*/', lambda m: re.sub(r'[^\n]+', '', m.group()), text, flags=re.S)
print(text)

This prints:

Keep this 

This stays on line number 3

The replacement logic in the lambda function operates on the /* ... */ comment block. It strips off all characters except for newlines, leaving the newline structure intact while removing all other content from the intermediate comment lines.

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