Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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/4/maven/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 如何使用正则表达式跨多行解析日志文件中的文本?_Python_Regex_Parsing - Fatal编程技术网

Python 如何使用正则表达式跨多行解析日志文件中的文本?

Python 如何使用正则表达式跨多行解析日志文件中的文本?,python,regex,parsing,Python,Regex,Parsing,我有一个日志输出,总结如下。我需要解析最终的输入,它跨越多行。我找不到有效的正则表达式 04/10/2019 02:52:59 PM INFO: Model Details: 04/10/2019 02:53:12 PM INFO: Final Input: [ 220.12134 3.7499998 75.00001 111.44428 22.500004 37.5 73.361534 1000.709 ] 04/10/2

我有一个日志输出,总结如下。我需要解析最终的输入,它跨越多行。我找不到有效的正则表达式

04/10/2019 02:52:59 PM INFO: Model Details:
04/10/2019 02:53:12 PM INFO: Final Input: [  220.12134       3.7499998    75.00001     111.44428      22.500004
    37.5          73.361534  1000.709    ]
04/10/2019 02:53:12 PM INFO: Difference: [ 11.974823 647.91406 ]
04/10/2019 02:53:12 PM INFO: Number: 169
04/10/2019 02:53:12 PM INFO: Time: 13.554227686000004 seconds

我想要一个numpy数组输出:

[220.12134, 3.7499998, 75.00001, 111.44428, 22.50000437.5, 73.361534, 1000.709]

使用以下代码,我可以使其适用于单行:

log_file_path = some_log.log
#regex = '\[(.*?)\]'
regex2 = '(Final Input: \[)(.*?)(\]|\n)'

with open(log_file_path, 'r') as file:
    all_log_file = file.read()
    a = re.findall(regex2, all_log_file)
    print(a)

file.close()
#x = list(map(float, a.split()))
我得到以下输出,它缺少下一行的最终输入值(我可以将下面的输出解析为numpy数组):

使用非贪婪说明符以及
re.DOTALL
,这表示
包括
\n

import re

regex2 = '(Final Input: \[.+?\])'

a = re.findall(regex2, text, re.DOTALL)
a
输出:

['Final Input: [  220.12134       3.7499998    75.00001     111.44428      22.500004\n    37.5          73.361534  1000.709    ]']
['Final Input: [  220.12134       3.7499998    75.00001     111.44428      22.500004\n    37.5          73.361534  1000.709    ]']