如何使用python正则表达式查找和替换句子中第n个单词的出现?

jug*_*aut 14 python regex

仅使用python正则表达式,如何查找和替换句子中第n个单词的出现?例如:

str = 'cat goose  mouse horse pig cat cow'
new_str = re.sub(r'cat', r'Bull', str)
new_str = re.sub(r'cat', r'Bull', str, 1)
new_str = re.sub(r'cat', r'Bull', str, 2)
Run Code Online (Sandbox Code Playgroud)

我上面有一句话,"cat"这个词出现在句子中两次.我希望第二次出现的'猫'改为'公牛',留下第一个'猫'字.我的最后一句话看起来像是:"猫鹅鼠马猪公牛".在我上面的代码中,我试过3次不能得到我想要的东西.

Avi*_*Raj 15

使用负面预测,如下所示.

>>> s = "cat goose  mouse horse pig cat cow"
>>> re.sub(r'^((?:(?!cat).)*cat(?:(?!cat).)*)cat', r'\1Bull', s)
'cat goose  mouse horse pig Bull cow'
Run Code Online (Sandbox Code Playgroud)

DEMO

  • ^ 断言我们刚开始.
  • (?:(?!cat).)*匹配任何字符,但不匹配cat,零次或多次.
  • cat匹配第一cat个子字符串.
  • (?:(?!cat).)*匹配任何字符,但不匹配cat,零次或多次.
  • 现在,将所有模式包含在捕获组中((?:(?!cat).)*cat(?:(?!cat).)*),以便我们可以在以后引用捕获的字符.
  • cat现在cat匹配以下第二个字符串.

要么

>>> s = "cat goose  mouse horse pig cat cow"
>>> re.sub(r'^(.*?(cat.*?){1})cat', r'\1Bull', s)
'cat goose  mouse horse pig Bull cow'
Run Code Online (Sandbox Code Playgroud)

更改其中的数字{}以替换字符串的第一个或第二个或第n个匹配项cat

要替换第三次出现的字符串cat,请放入2花括号内..

>>> re.sub(r'^(.*?(cat.*?){2})cat', r'\1Bull', "cat goose  mouse horse pig cat foo cat cow")
'cat goose  mouse horse pig cat foo Bull cow'
Run Code Online (Sandbox Code Playgroud)

在这里玩上面的正则表达式...

  • @AvinashRaj:人们可以投票反对,因为这是一个过于复杂的答案。(顺便说一句不是我的,顺便说一句)。 (2认同)

ale*_*kva 5

我使用简单的函数,该函数列出所有出现的事件,选择第n个位置,并使用它将原始字符串分成两个子字符串。然后,它替换第二个子字符串中的第一个匹配项,并将子字符串连接回新的字符串中:

import re

def replacenth(string, sub, wanted, n)
    where = [m.start() for m in re.finditer(sub, string)][n-1]
    before = string[:where]
    after = string[where:]
    after.replace(sub, wanted, 1)
    newString = before + after
    print newString
Run Code Online (Sandbox Code Playgroud)

对于这些变量:

string = 'ababababababababab'
sub = 'ab'
wanted = 'CD'
n = 5
Run Code Online (Sandbox Code Playgroud)

输出:

ababababCDabababab
Run Code Online (Sandbox Code Playgroud)

笔记:

where变量实际上是匹配项位置的列表,您在第n个位置。但是列表项索引0通常以开头,而不是以开头1。因此,有一个n-1索引,n变量是实际的第n个子字符串。我的示例找到第5个字符串。如果您使用nindex并希望找到第5位,则需要n4。您通常使用哪种功能取决于生成我们的函数n

这应该是最简单的方法,但它不仅仅是您最初想要的正则表达式。

来源和一些链接: