gpt4 book ai didi

python - 仅删除引号中句子的特定部分

转载 作者:行者123 更新时间:2023-12-01 01:52:22 25 4
gpt4 key购买 nike

我正在努力删除字符串的特定部分,而且我对正则表达式相当陌生。我想删除字符串中引号中包含人名的部分,“断开连接”和“由客户结束”行。我正在研究的一个句子的示例是:

new_text = "this is the 'ideal' problem 'joe smith' disconnected ('Concluded by customer')."
re.sub(r"\s'([\w\W\d]+)' disconnected \(.*\)[.|\s]*", '', new_text)

结果显示:

"this is the"

但我想得到:

"this is the 'ideal' problem"

关于如何更改我的正则表达式模式有什么想法吗?

最佳答案

这是一种可能性:

import re

new_text = "this is the 'ideal' problem 'joe smith' disconnected ('Concluded by customer')."
result = re.sub(r"(^.*)\s+'[^']+' disconnected.*$", r"\1", new_text)
print(result)

输出:

this is the 'ideal' problem

关于python - 仅删除引号中句子的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50587373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com