更新时间: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
要替换字符串 cat
的第三次出现,请将 2
放在花括号内..
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'
^
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.((?:(?!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'