且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-21 14:11:22

使用如下所示的否定前瞻.

>>>s = "猫鹅老鼠马猪猫牛">>>re.sub(r'^((?:(?!cat).)*cat(?:(?!cat).)*)cat', r'\1Bull', s)'猫鹅老鼠马猪牛牛'

演示

  • ^ 断言我们处于开始阶段.
  • (?:(?!cat).)* 匹配除 cat 之外的任何字符,零次或多次.
  • cat 匹配第一个 cat 子字符串.
  • (?:(?!cat).)* 匹配除 cat 之外的任何字符,零次或多次.
  • 现在,将所有模式包含在一个捕获组中,例如 ((?:(?!cat).)*cat(?:(?!cat).)*),以便我们以后可以参考那些捕获的字符.
  • cat 现在匹配第二个 cat 字符串.

>>>s = "猫鹅老鼠马猪猫牛">>>re.sub(r'^(.*?(cat.*?){1})cat', r'\1Bull', s)'猫鹅老鼠马猪牛牛'

更改 {} 内的数字以替换字符串 cat

的第一次或第二次或第 n 次出现

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

>>>re.sub(r'^(.*?(cat.*?){2})cat', r'\1Bull', "cat goose mouse horse pig cat foo cat cow")'猫鹅老鼠马猪猫foo公牛'

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

Using python regular expression only, how to find and replace nth occurrence of word in a sentence? For example:

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)

I have a sentence above where the word 'cat' appears two times in the sentence. I want 2nd occurence of the 'cat' to be changed to 'Bull' leaving 1st 'cat' word untouched. My final sentence would look like: "cat goose mouse horse pig Bull cow". In my code above I tried 3 different times could not get what I wanted.

Use negative lookahead like below.

>>> 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'

DEMO

  • ^ Asserts that we are at the start.
  • (?:(?!cat).)* Matches any character but not of cat , zero or more times.
  • cat matches the first cat substring.
  • (?:(?!cat).)* Matches any character but not of cat , zero or more times.
  • Now, enclose all the patterns inside a capturing group like ((?:(?!cat).)*cat(?:(?!cat).)*), so that we could refer those captured chars on later.
  • cat now the following second cat string is matched.

OR

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

Change the number inside the {} to replace the first or second or nth occurrence of the string cat

To replace the third occurrence of the string cat, put 2 inside the curly braces ..

>>> 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'

Play with the above regex on here ...