In [10]: title = "[test][R] D123/Peace123456: panic:"
# `^\[[^]]*\]` matches `[` followed by any character
# except `]` zero or more times, followed by `]`
In [11]: re.sub(r'^\[[^]]*\]', '', title)
Out[11]: '[R] D123/Peace123456: panic:'
# `^\[.*?\]` matches `[`, followed by any number of
# characters non-greedily by `.*?`, followed by `]`
In [12]: re.sub(r'^\[.*?\]', '', title)
Out[12]: '[R] D123/Peace123456: panic:'
您需要非贪婪正则表达式来匹配开始时的第一个
[]
,并re.sub
来进行替换:相关问题 更多 >
编程相关推荐