小编Ere*_*ezO的帖子

Python Regex - 替换不在两个特定单词之间的字符串

给定一个字符串,我需要在不在两个给定单词之间的区域中用另一个子串替换子串.

例如:

substring: "ate" replace to "drank", 1st word - "wolf", 2nd word - "chicken"

input:  The wolf ate the chicken and ate the rooster
output: The wolf ate the chicken and drank the rooster
Run Code Online (Sandbox Code Playgroud)

目前,我唯一的解决方案是非常不洁净:

1)通过替换位于其间的字符串,将位于两个单词之间的字符串替换为临时子字符串

2)替换我原来想要的字符串

3)将临时字符串还原为原始字符串

编辑:

我特别提出了一个与我的案例略有不同的问题,以保持答案与未来的读者相关.

我特别需要根据":"拆分一个字符串,当我需要忽略"<"和">"括号之间可以链接的":"时,唯一的承诺是开口括号的数量等于关闭括号的数量.

例如,在以下情况中:

input  a : <<a : b> c> : <a < a < b : b> : b> : b> : a
output [a, <<a : b> c>, <a < a < b …
Run Code Online (Sandbox Code Playgroud)

python regex

7
推荐指数
1
解决办法
179
查看次数

标签 统计

python ×1

regex ×1