【问题标题】:Extract from text with python and regex使用 python 和正则表达式从文本中提取
【发布时间】:2016-11-19 20:19:08
【问题描述】:

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

用户:报价

我们可以在一个文本中有多个引号。

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 中做到这一点?

【问题讨论】:

标签: python regex python-3.x extract


【解决方案1】:

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

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

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

这是regex101 上的演示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2015-01-16
    • 2011-11-15
    相关资源
    最近更新 更多