搜索下一行

问题描述 投票:-2回答:2

我需要编写一个python正则表达式,它匹配匹配单词旁边的单词。但是下一个单词可以在下一行中,也可以是同一行。

例如:

"""
Running /health_checks/system_checks1     [ FAIL ] 
Running /health_checks/system_checks2       [ PASS ] 
Running /health_checks/system_checks3           
                                         [ PASS ] 
"""

在system_checks3之后有新行,然后是检查结果。

我想要的结果如下:

system_checks2 PASS
system_checks3 PASS 

等等

python regex newline
2个回答
0
投票

我认为最好的方法是首先使用不同的输出格式。使用正则表达式解析输出很少是一个好主意,代码只是打破了太容易:在某些时候输出将被更改,您的程序将中断,并在一个月后再次读取正则表达是一个令人头疼的问题。

假设您无法更改输出格式,那么我仍然建议不要使用正则表达式。您可能想要查看python函数split(),这是一个使用它的版本:

string="""
Running /health_checks/system_checks1     [ FAIL ] 
Running /health_checks/system_checks2       [ PASS ] 
Running /health_checks/system_checks3           
                                         [ PASS ] 
"""

# remove newlines
string = string.replace("\n", "")

# split into individual jobs
jobs=string.split("Running")

# remove empty strings
jobs=[job for job in jobs if job!=""]

# take only the part with the result
results=[]
for job in jobs:
    # separate in the process name and the result
    # assuming the result is always wrapped in []
    # then [ can be used as a delimiter
    splitted=job.split("[")

    # splitted contains the job name and the result
    result=splitted[1]

    # remove the trailing ]
    result=result.replace("]","")

    # remove whitespace
    result=result.strip()

    results.append(result)

results数组现在包含:

['失败','通过','通过']


0
投票

虽然正则表达式有时不是文本处理的最佳选择,但在这种情况下它没有任何问题。

但是下一个单词可以在下一行中,也可以是同一行。

没问题,\s匹配任何空格字符,包括\n

string="""
Running /health_checks/system_checks1     [ FAIL ] 
Running /health_checks/system_checks2       [ PASS ] 
Running /health_checks/system_checks3           
                                         [ PASS ] 
"""
import re
result = re.findall("(\w+)\s*\[ (PASS) ]", string)
for tuple in result: print ' '.join(tuple)
© www.soinside.com 2019 - 2024. All rights reserved.