如何使用python正则表达式查找和替换句子中第n次出现的单词? [英] How to find and replace nth occurrence of word in a sentence using python regular expression?

查看:105
本文介绍了如何使用python正则表达式查找和替换句子中第n次出现的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

str = '猫鹅老鼠马猪猫牛'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)

我上面有一个句子,其中cat"这个词在句子中出现了两次.我希望将猫"的第二次出现更改为公牛",而保持第一个猫"字不变.我的最后一句话看起来像:猫鹅老鼠马猪牛牛".在我上面的代码中,我尝试了 3 次不同的时间都没有得到我想要的.

解决方案

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

<预><代码>>>>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 ...

这篇关于如何使用python正则表达式查找和替换句子中第n次出现的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆