我试图使用这个正则表达式从字符串中删除方括号(及其中的所有内容)的所有实例.例如,当字符串中只有一对方括号时,这种方法有效:
import re
pattern = r'\[[^()]*\]'
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens."""
t = re.sub(pattern, '', s)
print t
Run Code Online (Sandbox Code Playgroud)
我得到的是正确的:
>>>Issachar is a rawboned donkey lying down among the sheep pens.
Run Code Online (Sandbox Code Playgroud)
但是,如果我的字符串包含多个方括号,则它不起作用.例如:
s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""
Run Code Online (Sandbox Code Playgroud)
我明白了:
>>>Issachar is a rawboned
Run Code Online (Sandbox Code Playgroud)
无论字符串中有多少个方括号,我都需要使用正则表达式.正确的答案应该是:
>>>Issachar is a rawboned donkey lying down among the sheep pens.
Run Code Online (Sandbox Code Playgroud)
我研究并尝试了许多排列无济于事.
默认情况下*(或+)贪婪地匹配,因此问题中给出的模式将匹配到最后一个].
>>> 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]']
Run Code Online (Sandbox Code Playgroud)
通过?在重复运算符(*)之后附加,可以使其与非贪婪方式匹配.
>>> 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.'
Run Code Online (Sandbox Code Playgroud)
尝试:
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
Run Code Online (Sandbox Code Playgroud)
输出:
Issachar is a rawboned donkey lying down among the sheep pens.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5095 次 |
| 最近记录: |