Python正则表达式删除所有方括号及其内容

时间:2017-02-19 06:45:48

标签: python regex

我正在尝试使用此正则表达式从字符串中删除方括号(及其中的所有内容)的所有实例。例如,当字符串中只有一对方括号时,这种方法有效:

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

我得到的是正确的:

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

但是,如果我的字符串包含多个方括号,则它不起作用。例如:

s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""

我明白了:

>>>Issachar is a rawboned

无论字符串中有多少个方括号,我都需要使用正则表达式。正确的答案应该是:

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

我研究并尝试了许多排列无济于事。

3 个答案:

答案 0 :(得分:6)

默认情况下,*(或+)会贪婪地匹配,因此问题中给出的模式将与上一个]匹配。

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

答案 1 :(得分:4)

尝试:

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.

答案 2 :(得分:0)

用于括号内的数字(无字母),例如[89],[23],[11]等, 这就是要使用的模式。

import re

text = "The[TEXT] rain in[33] Spain[TEXT] falls[12] mainly in[23] the plain![45]"
pattern = "\[\d*?\]"
numBrackets = re.findall(pattern, text)

print(numBrackets)

输出:

['[33]', '[12]', '[23]', '[45]']