【发布时间】:2020-10-21 11:02:49
【问题描述】:
我正在尝试分析一篇文章以确定是否出现了特定的子字符串。
如果出现"Bill",那么我想从文章中删除子字符串的父句子,以及第一个删除句子之后的每个句子。
如果"Bill"没有出现,则文章不做任何改动。
示例文本:
stringy = """This is Bill Everest here. A long time ago in, erm, this galaxy, a game called Star Wars Episode I: Racer was a smash hit, leading to dozens of enthusiastic magazine reviews with the byline "now this is podracing!" Unfortunately, the intervening years have been unkind to the Star Wars prequels, Star Fox in the way you can rotate your craft to fit through narrow gaps.
This is Bill, signing off. Thank you for reading. And see you tomorrow!"""
目标子串为“Bill”时的期望结果:
stringy = """This is Bill Everest here. A long time ago in, erm, this galaxy, a game called Star Wars Episode I: Racer was a smash hit, leading to dozens of enthusiastic magazine reviews with the byline "now this is podracing!" Unfortunately, the intervening years have been unkind to the Star Wars prequels, but does that hindsight extend to this thoroughly literally-named racing tie-in? Star Fox in the way you can rotate your craft to fit through narrow gaps.
"""
这是目前为止的代码:
if "Bill" not in stringy[-200:]:
print(stringy)
text = stringy.rsplit("Bill")[0]
text = text.split('.')[:-1]
text = '.'.join(text) + '.'
当"Bill" 出现在最后 200 个字符之外时,它目前不起作用,在"Bill" 的第一个实例处截断文本(开头句,"This is Bill Everest here")。如何将此代码更改为仅选择最后 200 个字符中的 "Bill"s?
【问题讨论】:
标签: python python-3.x algorithm substring re