2017-02-19 16 views
4

我想用這個正則表達式來從字符串中刪除所有方括號(和它們中的所有)的實例。例如,這個工程時,有字符串中只有一對括號組成:Python的正則表達式來刪除所有方括號和它們的內容

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. 

我已經研究並嘗試了許多排列無濟於事。

+0

請注意,雖然正則表達式可以幫助你在一系列匹配括號(如[B] C [d] E),他們一般不能對付所謂的[*嵌套的括號問題*](HTTP ://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns#133684)。 (例如:a [b [c] [d [e]]]。)但是,您可以通過專門編碼某些最大數量的嵌套來「僞造」它。 –

回答

3

默認情況下*(或+)貪婪地匹配,所以在這個問題給出的模式將匹配高達最後]

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

嘗試:

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. 
相關問題