gpt4 book ai didi

python - 删除 "not"之后的第一个单词

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:18 25 4
gpt4 key购买 nike

我有一个字符串:

s = "grocery store not liquor shop not restaurant, sell milk"

,我想删除“不”之后的第一个词。我怎样才能在 python 中实现它?我正在寻找如下结果:

"grocery store shop, sell milk"

"grocery store, sell milk"

如果删除“not”和任何标点符号/字符串结尾之间的所有单词也是可能的。

最佳答案

你可以这样做:

import re
s = "grocery store not liquor shop not restaurant, sell milk"

print (re.sub(r'\s+not \w+', '', s))

你会得到这个:

grocery store shop, sell milk

关于python - 删除 "not"之后的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305310/

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