【问题标题】:Regex match the first line after blank line正则表达式匹配空行后的第一行
【发布时间】:2022-01-25 20:10:22
【问题描述】:

我想检查blank line 之后的第一行,我尝试使用^$.* 但不起作用,

YYYY-MM-DD HH:II:SS Message Log
Message Log

Message Log
not empty line

所需的输出: ["YYYY-MM-DD HH:II:SS Message Log","Message Log"]

【问题讨论】:

  • I want to check the first line after blank line ...您的预期输出与此请求不匹配。请编辑并解决您的问题。
  • 你是说改标题?
  • 否,我想检查空行后的第一行意味着使用您的数据时预期的结果应该是"Message Log"(@之前没有空行987654327@).

标签: python regex


【解决方案1】:

你可以使用:

import re


pattern = re.compile("(?:^|\n\n)(.*)")
pattern.findall(data)

输出

['YYYY-MM-DD HH:II:SS Message Log', 'Message Log']

为了测试它,我将这些行保存在一个文件中:

YYYY-MM-DD HH:II:SS Message Log
Message Log

Message Log
not empty line

阅读data中的内容并将模式应用到它。

【讨论】:

  • 我收到了['', 'Message Log']
  • .* 替换为.+,因此空行不匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2021-10-22
  • 2011-02-14
  • 1970-01-01
相关资源
最近更新 更多