我正在寻找一个正则表达式的解决方案,以删除在关键短语出现后的其余句子中的任何单词。
示例
句子=“今天的天气预报主要是晴天。明天的天气预报是下雨。这周剩下的时间.”
Key_phrase =“明天”
预期产量=“今天的天气预报主要是晴天。天气预报。这周剩下的时间.”
尝试
head, sep, tail = sentence.partition(key_phrase)
print(head)我的想法是先将字符串拆分成句子,然后应用上述技巧,然后加入结果。然而,我觉得一定有一个更优雅的方法来做这个与regex?
谢谢你的帮助
发布于 2021-05-27 13:36:41
使用re.sub
Ex:
sentence = "The weather forecast for today is mostly sunny. The forecast for tomorrow will be rainy. The rest of the week..."
key_phrase = "for tomorrow"
print(re.sub(fr"({key_phrase}.*?)(?=\.)", "", sentence))输出
The weather forecast for today is mostly sunny. The forecast . The rest of the week...发布于 2021-05-27 20:39:36
使用
re.sub(fr"{re.escape(key_phrase)}[^.]*", "", sentence)见正则证明。
解释
--------------------------------------------------------------------------------
for 'for'
--------------------------------------------------------------------------------
\ ' '
--------------------------------------------------------------------------------
tomorrow 'tomorrow'
--------------------------------------------------------------------------------
[^.]* any character except: '.' (0 or more times
(matching the most amount possible))请参阅Python证明
import re
sentence = "The weather forecast for today is mostly sunny. The forecast for tomorrow will be rainy. The rest of the week..."
key_phrase = "for tomorrow"
print(re.sub(fr"{re.escape(key_phrase)}[^.]*", "", sentence))结果:The weather forecast for today is mostly sunny. The forecast . The rest of the week...
https://stackoverflow.com/questions/67723222
复制相似问题