我想用這個正則表達式來從字符串中刪除所有方括號(和它們中的所有)的實例。例如,這個工程時,有字符串中只有一對括號組成: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.
我已經研究並嘗試了許多排列無濟於事。
請注意,雖然正則表達式可以幫助你在一系列匹配括號(如[B] C [d] E),他們一般不能對付所謂的[*嵌套的括號問題*](HTTP ://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns#133684)。 (例如:a [b [c] [d [e]]]。)但是,您可以通過專門編碼某些最大數量的嵌套來「僞造」它。 –