Python regex to remove all square brackets and their contents

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.

+4
source share
2 answers

* ( +) , , , ].

>>> 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]']

? (*), .

>>> 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.'
+3

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

:

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

Source: https://habr.com/ru/post/1670210/


All Articles