I am trying to use this regex to remove all instances of square brackets (and everything in them) from strings. For example, this works when there is only one pair of square brackets in a line:
import re
pattern = r'\[[^()]*\]'
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens."""
t = re.sub(pattern, '', s)
print t
I understood correctly:
>>>Issachar is a rawboned donkey lying down among the sheep pens.
However, if my line contains more than one set of square brackets, it does not work. For instance:
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""
I get:
>>>Issachar is a rawboned
I need the regex to work no matter how many square brackets are in the string. Correct answer:
>>>Issachar is a rawboned donkey lying down among the sheep pens.
I researched and tried many permutations to no avail.
source
share