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

How to remove only angular brackets from sentence using regex

I have tried one regex but that removes all the contents inside the brackets but i want that content.

Sentence: "Narendra Modi <N-Modi> is PM of India"

Output that i want : "Narendra Modi N-Modi is PM of India"

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

Code tried:

import re

#Replace all white-space characters with the digit "9":

txt = "Narendra Modi <N-Modi> is PM of India"
x = re.sub("\<[^\]]*\>", "", txt)
print(x)

>Solution :

I did not understand what you wanted, but if you have
"Narendra Modi is PM of India"
and you want output to be: "Narendra Modi N-Modi is PM of India"
hence replacing all angular brackets….

str = "Narendra Modi <N-Modi> is PM of India"
output = re.sub("[<>]", "", str)

print(output)

should do the work.

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