小编Chr*_*sen的帖子

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

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

python regex

6
推荐指数
2
解决办法
5095
查看次数

标签 统计

python ×1

regex ×1