【发布时间】:2018-10-09 02:59:40
【问题描述】:
我正在一个大字符串中搜索目标文本。我的代码选择字符串中的文本并在其前面显示 40 个字符和在其前面显示 40 个字符。相反,我希望在目标文本之前显示 2 个句子和 2 个句子。我的代码:
import re
sentence = "In addition, participation in life situations can be somewhat impaired because of communicative disabilities associated with the disorder and parents’ lack of resources for overcoming this aspect of the disability (i.e. communication devices). The attitudes of service providers are also important. The Australian Rett syndrome research program is based on a biopsychosocial model which integrates aspects of both medical and social models of disability and functioning. The investigation of environmental factors such as equipment and support available to individuals and families and the social capital of the communities in which they live is likely to be integral to understanding the burden of this disorder. The program will use the ICF framework to identify those factors determined to be most beneficial and cost effective in optimising health, function and quality of life for the affected child and her family."
sub = "biopsychosocial model"
def find_all_substrings(string, sub):
starts = [match.start() for match in re.finditer(re.escape(sub), string.lower())]
return starts
substrings = find_all_substrings(sentence, sub)
for pos in substrings: print(sentence[pos-40:pos+40])
如何在目标文本前面显示2句和在目标文本后面显示2句?
【问题讨论】: