Python regular expression to remove all square brackets and their contents

Try:

import re
pattern = r'\[[^\]]*\]'
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""
t = re.sub(pattern, '', s)
print t

Output:

Issachar is a rawboned donkey lying down among the sheep pens.

By default * (or +) matches greedily, so the pattern given in the question will match upto the last ].

>>> re.findall(r'\[[^()]*\]', "Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]")
['[a] donkey lying down among the sheep pens.[b]']

By appending ? after the repetition operator (*), you can make it match non-greedy way.

>>> import re
>>> pattern = r'\[.*?\]'
>>> s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""
>>> re.sub(pattern, '', s)
'Issachar is a rawboned donkey lying down among the sheep pens.'

Tags:

Python

Regex