Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression to remove all square brackets and their contents

Tags:

python

regex

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.

like image 883
Chris Nielsen Avatar asked Feb 19 '17 06:02

Chris Nielsen


People also ask

How do I remove all square brackets from a list in Python?

join() method to remove the square brackets from a list, e.g. result = ', '. join(str(item) for item in my_list) . The str. join() method will join the list into a string without the square brackets.

How do you remove contents from brackets in Python?

Method 1: We will use sub() method of re library (regular expressions). sub(): The functionality of sub() method is that it will find the specific pattern and replace it with some string. This method will find the substring which is present in the brackets or parenthesis and replace it with empty brackets.

How do I remove square brackets from text in Python?

In python, we can remove brackets with the help of regular expressions. # pattern is the special RE expression for finding the brackets.

How do you remove all parentheses in Python?

To remove parentheses from string using Python, the easiest way is to use the Python sub() function from the re module. If your parentheses are on the beginning and end of your string, you can also use the strip() function.


2 Answers

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.'
like image 146
falsetru Avatar answered Oct 14 '22 06:10

falsetru


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.
like image 39
Scovetta Avatar answered Oct 14 '22 06:10

Scovetta