首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何提取文本,直到它到达一个大写单词?Python

如何提取文本,直到它到达一个大写单词?Python
EN

Stack Overflow用户
提问于 2018-06-07 03:02:16
回答 1查看 632关注 0票数 2

这是我的完整文本:

RETENTION
Liability in excess of the Retention
The Retention shall be borne by the Named Insured and the Insurer shall only be liable for Loss once the Retention has been fully eroded. The Retention shall apply until such time as it has been fully eroded after which no Retention shall apply.
Erosion of the Retention
The Retention shall be eroded by Loss for which the Insurer would be liable under this Policy but for the Retention.

我想提取整个保留段。

这是我的代码,用来提取包含特定单词的句子(这里:保留)。

abc3=([sentence + '.' for sentence in txt_trim_string.split('.') if 'RETENTION' in sentence])

但这给出了如下输出:

RETENTION
Liability in excess of the Retention
The Retention shall be borne by the Named Insured and the Insurer shall only be liable for Loss once the Retention has been fully eroded.

我还想包括:

Erosion of the Retention
The Retention shall be eroded by Loss for which the Insurer would be liable under this Policy but for the Retention.

我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2018-06-07 05:05:47

尝试使用带有re.DOTALL选项的Regex:[A-Z]{2,}.*?(?=(?:[A-Z]{2,}|\Z))将换行符与.匹配

Demo

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50727776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档