繁体   English   中英

在 python 中使用 re.findall 匹配多行

[英]matching multiline using re.findall in python

在一个文件 Myfile: 中内容如下:

  username: prpadiya
  url: https://bhgerrit.ext.net.nokia.com:443/154588
  commitMessage: Handling for change in the path of cm library to /etc/opt/nokia/CMNMS/plugins.
                 NATP
  createdOn: 2020-05-22 12:52:52 IST

我需要从“commitMessage”开始匹配,直到提交消息中有任意数量的行。 在上面的文件中有额外的一行以“NATP”结尾。 我使用了 re.DOTALL 但仍然没有运气。 谁能帮我? 我的代码如下:

for line in myfile:
    if re.findall("^commitMessage:\s.*[\r\n].*", line, re.DOTALL):
        print("Line is ::", line)
        msg = line.split('commitMessage:')[-1]
        print("Msg is ::", msg)
        break

如果您需要跨行匹配某些模式,您应该将文件作为整个单个字符串读取,而不是逐行读取。 然后,您需要确保在commitMessage之后的每一行的开头都有水平空格。

使用myfile.read()

(?m)^commitMessage:.*(?:\n[^\S\n].*)*

请参阅正则表达式演示 细节:

  • (?m)^ - 行首( (?m)是内联修饰符,它执行re.M的作用,使^匹配行首, $匹配行尾)
  • commitMessage: - 一个字符串
  • .* - 行的 rest, .*匹配除换行符以外的任何 0 个或多个字符,尽可能多
  • (?:\n[^\S\n].*)* - 任何 0 次或多次重复:
    • \n - 换行符
    • [^\S\n] - LF 以外的空格
    • .* - 除换行符以外的任何 0 个或多个字符,尽可能多

Python:

with open(fpath, 'r') as myfile:
  print( re.findall(r'(?m)^commitMessage:.*(?:\n[^\S\n].*)*', myfile.read()) )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM