【发布时间】:2018-07-18 17:52:55
【问题描述】:
目前我正在遍历目录以查找/存储/显示包含版本号信息的最新日志行。我正在使用 Regex 查找带有版本号的日志行,并且我正在尝试通过将它们与 parse_version 进行比较来找到具有最新时间戳的日志行。
例如,我文件夹的文件中的日志行如下所示:
2018-05-08T15:47:27.752Z 00000000-0000-0000-0000-000000000000 > LVL:2 RC: version: 2.12.1.10452
2018-05-08T21:27:14.2049217Z> <INFO >: Version: 2.10.0.23960
2018-05-08T21:18:53.0428568Z> <INFO >: Version: 2.12.1.26051
这些只是我文件夹文件中数千条日志行的几个示例,我正在尝试查找包含版本号信息的最新日志行。在这种情况下,我希望选择第二行,即使它的版本号较低,因为它具有更新的时间戳。
下面是我的代码,为了简单起见,我没有包含循环遍历文件夹的代码。
for line in f: #For simplicity sake, I won't include my code above this line because it's just for looping through the folder to find the log lines
#0strip out \x00 from read content, in case it's encoded differently
line = line.replace('\x00', '')
#Regular expressions for finding the log lines in the folder
RE2 = r"^.+INFO.+Version.+"
RE3 = r"^.+RC: version"
previous_version_line = '0'
version_to_display = '00'
#Find the general matches, and get the version line with the latest time stamp
pattern2 = re.compile('('+RE2+'|'+RE3+')', re.IGNORECASE)
for match2 in pattern2.finditer(line):
if parse_version(line) > parse_version(previous_version_line):
version_to_display = line
previous_version_line = line
else:
version_to_display = previous_version_line
print(version_to_display)
现在问题似乎在于 parse_version 比较,虽然通过正则表达式找到的日志行的值应该高于 0,但 if 语句总是评估为 false,我只是打印一堆 0。
提前致谢!
【问题讨论】:
标签: regex python-3.x comparison