什么是正确的正则表达式?

时间:2010-11-10 19:04:14

标签: python regex replace

这项任务的正则表达式是什么? - > 将“[[...:”替换为“[[”

也就是说,我想在[[...:with [[。

。)中替换* some text *

我的代码的问题是它删除了第一个[[]]

中的* text *
>>> string = "Some text here [[dont remove me]] and some extra text [[remove me:and let this]] here."
>>> clean = re.sub(r'\[\[.+:', '[[', string)
>>> clean
'Some text here [[and let this]] here.'
>>>

2 个答案:

答案 0 :(得分:1)

re.sub(r'\[\[[^:\]]+:', '[[', string)
使用

[^:\]]代替.来限制要移除的内容限制在标记内。

答案 1 :(得分:0)

而不是.使用排除:和结束]]的表达式:

r'\[\[(?:[^:\]]|\][^\]])*:'
相关问题