Home > database >  How to remove only angular brackets from sentence using regex
How to remove only angular brackets from sentence using regex

Time:04-05

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"

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)

CodePudding user response:

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.

CodePudding user response:

All you need to remove are the < and >, so write regex to match only those.

import re
txt = "Narendra Modi <N-Modi> is PM of India"
x = re.sub(r"<|>", "", txt)
print(x)
# 'Narendra Modi N-Modi is PM of India'
  • Related