替换字符串中第 n 次出现的子字符串 [英] Replace nth occurrence of substring in string
问题描述
我想替换字符串中第 n 次出现的子字符串.
必须有一些与我想做的事情相同的事情
mystring.replace("substring", 2nd)
实现这一目标最简单和最 Pythonic 的方法是什么?
为什么不重复:我不想将正则表达式用于这种方法,而且我发现的大多数类似问题的答案只是正则表达式剥离或非常复杂的函数.我真的想要尽可能简单而不是正则表达式的解决方案.
我使用简单的函数,它列出所有出现的位置,选择第 n 个位置并用它来将原始字符串拆分为两个子字符串.然后它替换第二个子字符串中的第一次出现并将子字符串连接回新字符串:
导入重新def replacenth(string, sub, want, n):where = [m.start() for m in re.finditer(sub, string)][n-1]before = string[:where]after = 字符串[其中:]after = after.replace(sub, 想要, 1)newString = 之前 + 之后打印(新字符串)
对于这些变量:
string = 'ababababababababab'子 = 'ab'想要 = 'CD'n = 5
输出:
ababababCDabababab
注意事项:
<块引用>where
变量实际上是匹配位置的列表,您可以在其中选择第 n 个位置.但是列表项索引通常以 0
开头,而不是以 1
开头.因此有一个 n-1
索引,n
变量是实际的第 n 个子串.我的示例找到第 5 个字符串.如果您使用 n
索引并想找到第 5 个位置,则需要 n
为 4
.您使用的通常取决于函数,它生成我们的 n
.
<块引用>
这应该是最简单的方式,但也许不是最Pythonic的方式,因为where
变量构造需要导入re
库.也许有人会找到更 Pythonic 的方式.
<块引用>
来源和一些链接:
<块引用>
where
构造:如何查找所有出现的子串?- 字符串拆分:https://www.daniweb.com/programming/software-development/threads/452362/replace-nth-occurrence-of-any-sub-string-in-a-string
- 类似问题:找到第n次出现的子串字符串
I want to replace the n'th occurrence of a substring in a string.
There's got to be something equivalent to what I WANT to do which is
mystring.replace("substring", 2nd)
What is the simplest and most Pythonic way to achieve this?
Why not duplicate: I don't want to use regex for this approach and most of answers to similar questions I found are just regex stripping or really complex function. I really want as simple as possible and not regex solution.
I use simple function, which lists all occurrences, picks the nth one's position and uses it to split original string into two substrings. Then it replaces first occurrence in the second substring and joins substrings back into the new string:
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 = after.replace(sub, wanted, 1)
newString = before + after
print(newString)
For these variables:
string = 'ababababababababab'
sub = 'ab'
wanted = 'CD'
n = 5
outputs:
ababababCDabababab
Notes:
The
where
variable actually is a list of matches' positions, where you pick up the nth one. But list item index starts with0
usually, not with1
. Therefore there is an-1
index andn
variable is the actual nth substring. My example finds 5th string. If you usen
index and want to find 5th position, you'll needn
to be4
. Which you use usually depends on the function, which generates ourn
.
This should be the simplest way, but maybe it isn't the most Pythonic way, because the
where
variable construction needs importingre
library. Maybe somebody will find even more Pythonic way.
Sources and some links in addition:
where
construction: How to find all occurrences of a substring?- string splitting: https://www.daniweb.com/programming/software-development/threads/452362/replace-nth-occurrence-of-any-sub-string-in-a-string
- similar question: Find the nth occurrence of substring in a string
这篇关于替换字符串中第 n 次出现的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!