gpt4 book ai didi

python - 正则表达式提取段落

转载 作者:行者123 更新时间:2023-12-01 06:06:58 26 4
gpt4 key购买 nike

我正在尝试用 Python 编写正则表达式来提取段落的一部分。

在下面的段落中,我想要提取的部分以粗体显示。

Proposal will boost bailout fund, inject cash into banks and cut Greek debt says reports.

我的正则表达式和输出如下,

>>> text = 'Proposal will boost bailout fund, inject cash into banks and cut Greek debt says reports.'
>>> pattern = re.compile(r'(boost bailout)+?([\s\S]*?)(debt)+?')
>>> print re.findall(pattern, text)

[('boost bailout', ' fund, inject cash into banks and cut Greek ', 'debt')]

虽然它确实提取了正确的部分,但是提取被分为元组中的 3 个部分而不是像下面这样的单行,是否正确?

[('boost bailout fund, inject cash into banks and cut Greek debt')]

最佳答案

来自文档:

If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

-- http://docs.python.org/library/re.html

如果您想要一场比赛,请执行以下操作:

#!/usr/bin/env python
import re
text = 'Proposal will boost bailout fund, inject cash into banks and cut Greek debt says reports.'
pattern = re.compile(r'boost bailout[\s\S]*?debt')
print re.findall(pattern, text)

关于python - 正则表达式提取段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7569069/

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