如何检查2个字符串格式

How to check 2 string format

我有一个字符串“[ERROR]: test_case_blebleble - FAILURE - 1 hr 47 min” 从控制台获取登录詹金斯

在我的控制台日志中,我有很多这样的消息。所以我想检查是否有类似这种格式的字符串 FAILURE - 1 hr 47 min 然后去获取此消息。 那么,在 python 我们如何才能得到它 请帮助我 = 谢谢

您可以通过以下方式使用:

# Give the pattern that you are searching:
pattern = re.compile("[ERROR]: test_case_blebleble - FAILURE")

# Give name of your file 
for line in open("yourfile.txt"):
    for match in re.finditer(pattern, line):
        print(line)

with open(r"yourfile.txt") as infile:
    for line in infile:
        line=line.strip()
        if "- FAILURE - 1 hr 47 min" in line:
            print(line)
infile.close() 

编辑

import re

with open(r"yourfile.txt") as infile:
    for line in infile:
        line=line.strip()
        z=re.match("^\"\[[a-zA-Z]+\]:\s[a-zA-Z]+_[a-zA-Z]+[0-9]+\s+-\sFAILURE\s-\s(0?[0-9]|1[0-9]|2[0-3])\shr\s(0?[0-9]|[1-5][0-9])\smin\"",line)
        if z:
            print(line)
infile.close()