-1

These strings contain brackets that contain numbers or words. What is the best way to clean this with regex?

old_string = "This is demo text.[1] Lorem ipsum.[2] Another one.[note 1]"

new_string = "This is demo text. Lorem ipsum. Another one."
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
kharoufeh
  • 51
  • 1
  • 3
  • Welcome to Stack Overflow. Don't forget to accept an answer (tick the check-mark next to an answer) if it answers your question. In this way your question stops from showing up as _unanswered_. Also up-vote good answers. Note that accepting and up-voting answers is the way to say _thanks_ on Stack Overflow (don't use comments for that). – As you're starting out here, please take the [tour](https://stackoverflow.com/tour), read about [what's on-topic](https://stackoverflow.com/help/on-topic), and have a look at [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask). – Ivo Mori Aug 04 '20 at 07:03

1 Answers1

0

You can use the following code

import re 
old_string = "This is demo text.[1] Lorem ipsum.[2] Another one.[note 1]"
new_string = re.sub("[\(\[].*?[\)\]]", "", old_string)
Akhilesh Pothuri
  • 215
  • 1
  • 7
  • 19