如何替换substring中的第二个字符?

发布于 2025-02-09 05:02:38 字数 753 浏览 2 评论 0原文

我有以下字符串:

text_one = str("\"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.")

text_two = str("\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said.\"")

我需要用$替换s/s的每个实例,但是 在给定单词中的s/s的实例。因此,输入/输出看起来像:

> Mississippi
> Mis$i$$ippi

我的想法是在每个“”角色之后做类似'之类的事情这。我还考虑创建一个列表来处理每个单词。

I have the following strings:

text_one = str("\"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.")

text_two = str("\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said.\"")

I need to replace every instance of s/S with $, but not the first instance of s/S in a given word. So the input/output would look something like:

> Mississippi
> Mis$i$ippi

My idea is to do something like 'after every " " character, skip first "s" and then replace all others up until " " character' but I have no idea how I might go about this. I also thought about creating a list to handle each word.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2025-02-16 05:02:38

使用re的解决方案:

import re

text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
text_two = '\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said."'


def replace(s):
    return re.sub(
        r"(?<=[sS])(\S+)",
        lambda g: g.group(1).replace("s", "$").replace("S", "$"),
        s,
    )


print(replace(text_one))
print(replace(text_two))

打印:

"A Ukrainian American woman who lives near Boston, Mas$achu$ett$, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Rus$ian attacks on Ukraine and the fear these attacks have engendered.


Many people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Rus$ian soldier$ overtake the area, the Boston-area woman said."

Solution with re:

import re

text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
text_two = '\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said."'


def replace(s):
    return re.sub(
        r"(?<=[sS])(\S+)",
        lambda g: g.group(1).replace("s", "
quot;).replace("S", "
quot;),
        s,
    )


print(replace(text_one))
print(replace(text_two))

Prints:

"A Ukrainian American woman who lives near Boston, Mas$achu$ett$, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Rus$ian attacks on Ukraine and the fear these attacks have engendered.


Many people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Rus$ian soldier$ overtake the area, the Boston-area woman said."
撩动你心 2025-02-16 05:02:38

您要做的第一件事是找到第一个s的索引

,然后将字符串拆分,以便在第一个s和其余的字符串之后获得两个单独的变量

,用美元符号替换第二个字符串中的所有S

接下来

test = "mississippi"
first_index = test.find("s")
tests = [test[:first_index+1], test[first_index+1:]]
tests[1] = tests[1].replace("s", "$")
result = ''.join(tests)
print(result)

The first thing you're going to want to do is to find the index of the first s

Then, you'll want to split the string so you get the string until after the first s and the rest of the string into two separate variables

Next, replace all of the s's in the second string with dollar signs

Finally, join the two strings with an empty string

test = "mississippi"
first_index = test.find("s")
tests = [test[:first_index+1], test[first_index+1:]]
tests[1] = tests[1].replace("s", "
quot;)
result = ''.join(tests)
print(result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文