Python: re.sub 删除不同情况下两个定界符之间的单词

Python: re.sub removing words between two delimiters under different cases

我有一个字符串,其中包含类似

的模式
"something [[remove|keep]] something [[keep]] something [[remove|remove|keep]] something" 

如何使用re.sub()或其他方法将此字符串编辑为

"something keep something keep something keep something"

"something [[keep]] something [[keep]] something [[keep]] something". 

(之后我可以删除 [[ ]]s。)

我试过使用for-loop硬编码,但是字符串太长,需要很长时间才能运行。

您可以使用正则表达式:(?<=[\[\|])[^\[]*?\|:

import re

s = "something [[remove|keep]] something [[keep]] something [[remove|remove|keep]] something"
print(re.sub("(?<=[\[\|])[^\[]*?\|", "", s))

输出:

something [[keep]] something [[keep]] something [[keep]] something

正则表达式 (?<=[\[\|])[^\[]*?\| 可以分解为三个部分 return 个子字符串...

  • (?<=[\[\|]) - 紧靠子字符串左侧的是 [|
  • [^\[]*? - 包含任何 (非贪婪) 不是 [
  • \| - 以 |
  • 结尾