
本文介绍一种基于负向先行断言的正则技巧,精准匹配并移除普通 `[tag]` 类标签,同时保留 `[animation]` 和 `[animations]` 这两类特例标签及其后续内容。
在文本清洗任务中,我们常需剥离形如 [tag1][tag2]... 的标记结构,仅保留中间的主体内容。但当某些特定标签(如 [Animation] 或 [Animations])需被保留而非清除时,简单地用 r'\[.*?\]' 全局替换就会失效。
核心思路是:只匹配那些“不以 Animation 或 Animations 开头”的方括号标签。这可通过正则的负向先行断言(negative lookahead) 实现:
import re pattern = r'\[(?!Animations?\]).*?\]'
- \[(?!Animations?\]) 表示:匹配一个 [,但其后不能紧跟着 Animation] 或 Animations];
- Animations? 中的 ? 使 s 变为可选,从而同时覆盖 Animation(单数)和 Animations(复数);
- .*?\] 非贪婪匹配至下一个 ],确保捕获完整标签(如 [tag10]、[xyz] 等)。
完整处理示例如下:
import re
lines = [
"[tag1][tag4] Desired string - with optional dash [tag10]",
"[tag1][tag2][tag3] Desired string [tag10]",
"[tag3][tag1][tag2][tag5] Desired - string (with suffix)",
"[tag2][tag5][tag4] [Animation] Target string [tag10]",
"[tag3][tag1][tag5][tag10][Animations](prefix)Desired - string (and suffix)"
]
pattern = r'\[(?!Animations?\]).*?\]'
cleaned = [re.sub(pattern, '', line).strip() for line in lines]
for s in cleaned:
print(repr(s))输出结果(已自动去除多余空格):
'Desired string - with optional dash' 'Desired string' 'Desired - string (with suffix)' '[Animation] Target string' '[Animations](prefix)Desired - string (and suffix)'
✅ 关键注意事项:
- 此方案严格区分大小写:[animation] 或 [ANIMATION] 仍会被移除;如需忽略大小写,应添加 re.IGNORECASE 标志,并将模式改为 r'\[(?!Animations?\])' + flags=re.IGNORECASE;
- Animations? 必须写在 (?!) 内部,且需确保 ] 是字面量闭合符——因此 Animations?\] 是正确写法,不可省略反斜杠;
- 若原始字符串含嵌套或转义括号(如 \[escaped\]),本方案不适用,需升级为解析器级处理;
- 实际使用中建议对 re.sub() 结果调用 .strip(),以统一清理首尾空白。
该方法简洁高效,无需分步匹配或复杂逻辑,是处理“多数排除、少数保留”类标签清洗任务的标准正则范式。










