【问题标题】:Searching the next line搜索下一行
【发布时间】:2016-12-14 13:01:14
【问题描述】:

我需要编写一个 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 

等等

【问题讨论】:

  • 你好像忘了问一个问题。帖子中没有说明问题。
  • 我想要这样的结果:system_checks2 PASS,system_checks3 PASS 等等
  • 我不认为正则表达式是一个很好的工具。最好的办法是首先使输出更有条理。如果那是不可能的,你仍然可以通过 split() 得到一些结果。例如,删除换行符,制作一个长字符串,然后调用 split("running") 似乎是个好主意。然后您可以轻松地在 [] 中查找子字符串
  • 如果我们按“Running”分割,那么检查的结果将在检查名称(system_checks3)的下一行。
  • 你是对的。这可以通过首先删除换行符来解决,这样一切都只是一个长字符串。我已经发布了一个可能对您有所帮助的答案。

标签: python regex newline


【解决方案1】:

我认为最好的方法是首先使用不同的输出格式。 使用正则表达式解析输出很少是一个好主意,代码太容易出错:在某些时候输出会改变,你的程序会出错,一个月后再次阅读正则表达式是一件令人头疼的事情。

假设您无法更改输出格式,那么我仍然建议不要使用正则表达式。您可能想查看 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 数组现在包含:

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

【讨论】:

    【解决方案2】:

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

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

    没问题,\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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-10
      • 2016-06-01
      • 2014-09-13
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多