0

How to replace all square brackets and their values with empty strings if you don't know how many square brackets are there?

data = "[a] 1 [b] test [c] other characters"

I want the data to be " 1 test other characters".

fitz
  • 540
  • 4
  • 11
  • 3
    ``import re; re.findall("\d+", data)`` ? – sushanth Aug 01 '20 at 07:49
  • Does this answer your question? [How to extract numbers from a string in Python?](https://stackoverflow.com/questions/4289331/how-to-extract-numbers-from-a-string-in-python) – sushanth Aug 01 '20 at 07:49
  • 1
    I guess, the question is not about numbers, but about brackets, because there can be anything inside. Can there be nested brackets though? – bereal Aug 01 '20 at 07:50
  • 1
    @Sushanth This would work if there are no digits inside the brackets, and the parts outside are only made of digits. – Thierry Lathuille Aug 01 '20 at 07:52
  • 1
    What you have shown as desired output does not match the description in the text. Why should the output be `[ 1 2 ]` and not `1 2`? – mkrieger1 Aug 01 '20 at 07:58
  • @mkrieger1 you are right, I have modified the question. – fitz Aug 01 '20 at 08:08
  • @bereal Yes, thanks for your reminding, i have modified the question. – fitz Aug 01 '20 at 08:20

3 Answers3

1

You can split your string on the bracket parts using a regex:

import re

data = "[a] 1 [b] 2 [c]"

parts = re.split(r'\[.*?\]', data)
# ['', ' 1 ', ' 2 ', '']
out = '[' + ' '.join(parts) + ']'
print(out)
# [  1   2  ]
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

Another verbose answer would be:

import re
data = "[a] 1 [b] test [c] other characters"

pattern = '\[.*?\]' #any character(s) with [ ]

unwanted = set(re.findall(pattern,data))

results = ' '.join(i for i in data.split() 
                   if i not in unwanted)

print(results)
# 1 test other characters

Updated
Addition condition
Problem > "[a] 1 [b] test [1] other [c]haracters" Due to .split() this will not be replaced


import re

data = "[a] 1 [b] test [1] other [c]haracters"

pattern = '\[.*?\]' #any character(s) with [ ]

print(re.sub(pattern,'',data))
# " 1  test  other  haracters"

To remove the extra spaces

_ = re.sub(pattern,'',data).split()
print(' '.join(i for i in _ if i))

# "1 test other haracters"
Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
  • 1
    Thanks, but if the data is "[a] 1 [b] test [c]other characters", the result will contain [c], because you use data.split(), if ']' not followed by space, the output will not be expected. – fitz Aug 01 '20 at 08:32
  • I did not know about that scenario! Let me update the code – Prayson W. Daniel Aug 01 '20 at 08:48
-1

Try regex substitute.

import re

data = "[a] 1 [b] 2 [c]"
new = re.sub("([[a-z]*])", "", data)
JarroVGIT
  • 4,291
  • 1
  • 17
  • 29