Python正则表达式将替换所有模式,除非它与重复模式相邻

2024-09-28 23:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是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,但有一个例外。当quickquick进行时,只有第一个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,然后在第二个过程中转换边缘大小写。但我希望有一个更优雅或明显的一次性解决方案。你知道吗


Tags: thelinequickthislazylongveryover
3条回答

您可以利用分组完成此任务,方法如下:

import re
txt1 = 'The quick brown fox jumps over the lazy dog.'
txt2 = 'The quick quick brown fox jumps over the quick lazy dog.'
out1 = re.sub(r'(quick)((\squick)*)',r'lazy\2',txt1)
out2 = re.sub(r'(quick)((\squick)*)',r'lazy\2',txt2)
print(out1) # The lazy brown fox jumps over the lazy dog.
print(out2) # The lazy quick brown fox jumps over the lazy lazy dog.

想法很简单:第一组用于第一个quick,第二组用于rest quicks。然后将其替换为lazy和第2组的内容。你知道吗

下面是一种使用^{}的方法,当前面没有相同的子字符串时,使用负的lookback来替换quick

import re
re.sub(r'(?<!quick\s)quick', 'slow', s)

使用共享示例:

s1 = 'The quick brown fox jumps over the lazy dog. '
s2 = 'The quick quick brown fox jumps over the quick lazy dog. This a very very very very long line.'

re.sub(r'(?<!quick\s)quick', 'slow', s1)
# 'The slow brown fox jumps over the lazy dog. '

re.sub(r'(?<!quick\s)quick', 'slow', s2)
# 'The slow quick brown fox jumps over the slow lazy dog. This a very very very very long line.'

正则表达式细分:

  • (?<!quick\s)quick

    • 负向后看(?<!quick\s)

      • quick按字面意思快速匹配字符(区分大小写)

      • \s匹配任何空格字符(等于[\r\n\t\f\v ]

    • quick按字面意思匹配字符quick(区分大小写)

下面是不支持look aheads的regex引擎的一个变体:

quick(( quick)*)

替换为

slow\1

相关问题 更多 >