Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用re.findall匹配多行_Python_Regex - Fatal编程技术网

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

在python中使用re.findall匹配多行,python,regex,Python,Regex,在文件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 for

在文件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
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”开始到commit消息中任意数量的行。在上面的文件中,还有一行以“NATP”结尾。我使用了re.DOTALL,但仍然没有运气。有人能帮我吗?我的代码如下:

  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
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
    的操作,使
    ^
    匹配行的开头和
    $
    匹配行的结尾)
  • 提交消息:
    -字符串
  • *
    -行的其余部分,
    *
    尽可能多地匹配除换行符以外的任何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()) )

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

使用
myfile.read()

(?m)^commitMessage:.*(?:\n[^\S\n].*)*
看。详情:

  • (?m)^
    -行的开头(
    (?m)
    是内联修饰符,它执行
    re.m
    的操作,使
    ^
    匹配行的开头和
    $
    匹配行的结尾)
  • 提交消息:
    -字符串
  • *
    -行的其余部分,
    *
    尽可能多地匹配除换行符以外的任何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()) )

看,,如果您在myfile:中有
for line,并且使用带有
re.DOTALL的正则表达式,您可能会犯错误。您可能应该一次读取整个文件并匹配完整的文件文本,或者逐行迭代文件,并在包含
'commitMessage'
的一行之后/包括该行之后捕获该行,直到您点击
'createOn'
看,如果您在myfile:
中有
for line,并且使用带有
re.DOTALL的正则表达式,您可能会犯错误。您可能应该一次读取整个文件并匹配完整的文件文本,或者逐行迭代文件,并在包含
'commitMessage'
的一行之后/包括该行之后捕获该行,直到您点击
'createOn'
您好,wiktor,感谢您的及时回复和解释。你的正则表达式在外面工作,我是说在网站“”中,但在我的vi编辑器中不是。我必须使用for循环读取一行,在这一行中,我需要匹配正则表达式。因为文件中有太多类似的提交消息。请在下一个注释中检查我的总代码,并将“review.txt”、“r”作为f1:if re.findall(数字,行):next(f1)next(f1)next(f1)name=next(f1)。split(':')[-1]emailid=next(f1)。split(':'))[-1]for line in f1:if re.findall(r'^commitMessage:.*(:\n[^\S\n].*),line,re.DOTALL:msg=line.split('commitMessage:'))[-1]打印(“msg is:”,msg)中断打印(name.strip()+“&+emailid.strip()+”&+msg)返回名称、电子邮件ID、,msg@SinghB它不会像那样工作。你不能逐行读取文件,然后运行一个设计用于匹配多行文本块的模式。同样,
re.DOTALL
在一行上运行正则表达式毫无意义。好的..我会检查。感谢你的支持Hi wiktor,感谢你的及时响应e和解释。你的正则表达式在外部工作,我的意思是在网站“”中,但在我的vi编辑器中不是。我必须使用for循环读取一行,在这行中我需要匹配正则表达式。因为文件中有太多类似的提交消息。请在下一个注释中用open(“review.txt”,“r”)检查我的全部代码as f1:对于f1中的行:if re.findall(number,line):next(f1)next(f1)name=next(f1).split(':')[-1]emailid=next(f1).split(':')[-1]对于f1中的行:if re.findall(r'^commitMessage:.*(:\n[^\S\n].*),line,re.DOTALL):msg=line.split('commitMessage:')[-1]打印(“msg is::”,msg)中断打印(name.strip()+“&”+emailid.strip()+“&”+msg)返回名称、电子邮件ID、,msg@SinghB它不会那样工作。你不能逐行读取文件,然后运行一个设计用于匹配多行文本块的模式。同样,
re.DOTALL
对单行运行正则表达式毫无意义。好的..我会检查。谢谢你的支持