gpt4 book ai didi

python - 使用 python 和正则表达式从文本中提取

转载 作者:太空宇宙 更新时间:2023-11-03 15:56:53 24 4
gpt4 key购买 nike

假设我们有一些文本,其中一些引号存储在表单中:

用户:引用

文本中可以有多个引号。

Agatha Drake: She records her videos from the future? What is she, a
f**ing time lord? Is she Michael J. Fox?

Harvey Spencer: This is just like that one movie where that one guy
changed one tiny, little thing in his childhood to stop the girl of
his dreams from being a crackhead in the future!

我如何从Python文本中提取引号(她录制了她的视频......,这就像那一部电影......)?

我试过了

re.findall('\S\:\s?(.*)', text)

但这并没有完成任务。

https://regex101.com/r/vH63Go/1

我怎样才能用Python做到这一点?

最佳答案

如果您的字符串在行首遵循用户的一致格式,并以双换行符结束引号,则可以使用以下内容:

(?m)^[^:\n]+:\s?((?:.+\n?)*)

它使用多行模式并匹配行的开头,后跟既不是 : 也不是换行符的字符,然后是 :。然后捕获以下所有包含内容的行。

这是 regex101 上的演示.

关于python - 使用 python 和正则表达式从文本中提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697477/

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