I am trying to use this regular expression 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 the string:
import re
pattern = r'[[^()]*]'
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens."""
t = re.sub(pattern, '', s)
print t
What I get is correct:
>>>Issachar is a rawboned donkey lying down among the sheep pens.
However, if my string contains more than one set of square brackets, it doesn't work. For example:
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""
I get:
>>>Issachar is a rawboned
I need the regular expression to work no matter how many square brackets are in the string. Correct answer should be:
>>>Issachar is a rawboned donkey lying down among the sheep pens.
I have researched and tried many permutations to no avail.
question from:
https://stackoverflow.com/questions/65943641/how-to-delete-everything-between-and-in-a-string-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…