我使用的是Python,我有一个多行字符串,看起来像:
The quick brown fox jumps over the lazy dog.
The quick quick brown fox jumps over the quick lazy dog. This a very very very very long line.
This line has other text?
The quick quick brown fox jumps over the quick lazy dog.
我想用slow
替换所有出现的quick
,但有一个例外。当quick
由quick
进行时,只有第一个quick
被第二个quick
转换,相邻的quick
保持不变。你知道吗
因此,输出应该如下所示:
The slow brown fox jumps over the lazy dog.
The slow quick brown fox jumps over the slow lazy dog. This a very very very very long line.
This line has other text?
The slow quick brown fox jumps over the slow lazy dog.
我可以使用多个过程来实现这一点,首先将所有内容转换为slow
,然后在第二个过程中转换边缘大小写。但我希望有一个更优雅或明显的一次性解决方案。你知道吗
您可以利用分组完成此任务,方法如下:
想法很简单:第一组用于第一个
quick
,第二组用于rest quicks。然后将其替换为lazy
和第2组的内容。你知道吗下面是一种使用^{} 的方法,当前面没有相同的子字符串时,使用负的lookback来替换
quick
:使用共享示例:
正则表达式细分:
(?<!quick\s)quick
负向后看
(?<!quick\s)
quick
按字面意思快速匹配字符(区分大小写)\s
匹配任何空格字符(等于[\r\n\t\f\v ]
)quick按字面意思匹配字符quick(区分大小写)
下面是不支持look aheads的regex引擎的一个变体:
替换为
相关问题 更多 >
编程相关推荐