如何替换字符串末尾的某些字符?
我想替换 python 字符串末尾的字符。我有这个字符串:
s = "123123"
我想将最后一个 2
替换为 x
。假设有一个名为 replace_last
的方法:
>>> replace_last(s, '2', 'x')
'1231x3'
是否有任何内置或简单的方法可以执行此操作?
它类似于 python 的 str.replace() :
>>> s.replace('2', 'x', 1)
'1x3123'
但它是从头到尾。
I want to replace characters at the end of a python string. I have this string:
s = "123123"
I want to replace the last 2
with x
. Suppose there is a method called replace_last
:
>>> replace_last(s, '2', 'x')
'1231x3'
Is there any built-in or easy method to do this?
It's similar to python's str.replace()
:
>>> s.replace('2', 'x', 1)
'1x3123'
But it's from the end to beginning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用正则表达式函数
re.sub
替换字符串末尾的单词Prints:
或
Prints:
Using regular expression function
re.sub
to replace words at end of stringPrints:
or
Prints:
这正是
rpartition
函数用于:我编写了这个函数,展示了如何在您的用例中使用 rpartition:
输出:
This is exactly what the
rpartition
function is used for:I wrote this function showing how to use
rpartition
in your use case:Output:
这是少数没有左右版本的字符串函数之一,但我们可以使用一些有左右版本的字符串函数来模拟行为。
或者
This is one of the few string functions that doesn't have a left and right version, but we can mimic the behaviour using some of the string functions that do.
or
对于你的第二个例子
For your second example
当想要的匹配位于字符串末尾时,
re.sub
来救援。
When the wanted match is at the end of string,
re.sub
comes to the rescue.这是基于对您的问题的简单解释的解决方案。更好的答案需要更多信息。
更新
(看到编辑后的问题后)另一个非常具体的答案。
更新2
有更好的这样做的方法。由 @mizipzor 提供。
Here is a solution based on a simplistic interpretation of your question. A better answer will require more information.
Update
(After seeing edited question) another very specific answer.
Update 2
There is far better way to do this. Courtesy @mizipzor.
有一种方法可以处理替换以使其向后工作。
这个想法是使用
[::-1]
反转字符串、要替换的文本和新文本。逻辑保持不变。例如:
我们想要用“abc”替换最后一次(或两次或n次)出现的“123”,
这可以作为一个行为与replace完全相同的函数。
执行:
There is a way to handle
replace
to make it work backwards.The idea is to reverse the string, text to replace and new text using
[::-1]
. The logic stays the same.For example:
and we want to replace the last one, (or two, or n) occurrences of "123" with "abc"
This could be put as a function that behaves exactly like replace.
Execution:
有一个非常简单的方法可以做到这一点,按照我的步骤
首先,我们找到 am 最后一次出现的索引号,然后将第 0 个索引指向该特定索引。
There is a very easy way to do this, follow my steps
Here at first, we find the index number of am's last occurrence, then take 0th index to that particular index.
我得到了一个棘手的答案,但它不够高效
内置函数replace()需要三个参数
str.replace(旧的,新的,max_time)
所以你可以从原始字符串中删除最后一个匹配的字符串
I got a tricky answer, but it is not efficient enough
Built-in function replace() takes three arguments
str.replace(old, new, max_time)
So you can delete the last mached string from the origional string
完整的解决方案
str.rfind
和索引,处理不匹配的情况:A complete solution with
str.rfind
and indexing, handling the no-matches case:对于第二个例子,我推荐
rsplit
,因为它使用起来非常简单,并且直接达到了目标。这是它的一个小功能:输出:
For the second example, I would recommend
rsplit
, as it is very simple to use and and directly achieves the goal. Here is a little function with it:Output: