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

python remove unknown text inside specific mark

I have this text
text = "{\g1}Hello world!{\i6}"
I want to remove the curly brackets and the text inside them without knowing what is inside the curly brackets,
I know how to remove a specific text by using .replace("Hello", "Hi")
(sorry for my English)

>Solution :

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

You can use regular expressions to find & replace a particular pattern of characters.

In this case, a suitable regex pattern would be {\\.{2}}.

  • { matches a "{" character
  • \\ matches a "\" character
  • . matches any character except line breaks
  • {2} match exactly 2 of the preceding token (. in this case)
  • } matches a "}" character

So your python script would be:

import re

PATTERN = r'{\\.{2}}'

text = '{\g1}Hello world!{\i6}'

clean_text = re.sub(PATTERN, '', text)

print(clean_text)

And it would produce the output:

Hello world!
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