【问题标题】:Regexp match the output , while it shouldn't正则表达式匹配输出,而它不应该
【发布时间】:2019-03-26 12:30:22
【问题描述】:

我的输出如下所示:

+------+--------------+---------+------------+-----------------------+
| id   | IP address   | status  | type       | created at            |
+------+--------------+---------+------------+-----------------------+
| sc-1 |              | running | r4.2       | Aug 21, 2017 08:09:44 |
| sc-2 | 164.54.39.30 | running | r4.2       | Aug 21, 2017 08:09:44 |
+------+--------------+---------+------------+-----------------------+

我需要检查 sc-1 和 sc2 是否“正在运行”。

我对第一行的解决方案如下:

proc check_if_exist_in_output{lines    
String_to_Check} {
set err_msg ""
set Flag "False"
foreach line $lines { if { $line eq $String_to_Check} {
                    set Flag "True"
        return $Flag }}
if {$Flag == "False"} {return "Error"} 

现在这工作正常,但问题是,我可能在第一行也有这个 IP,然后我的脚本就不起作用了。

所以我尝试使用 REGEXP 的解决方案,我想从输出中提供一行,并检查它是否包含我正在寻找的行。

长话短说: 如果"| sc-2 | | running |" is part of line " | sc-2 | 164.54.39.30 | running | " 答案应该是真的。

我使用 REEXP 的新 proc 如下所示:

proc check_if_exist_in_output_with_reg_exspression {lines Str} { 
            set Flag "False"
    foreach line $lines {if {[regexp "$line.*" $Str] == 1}{                         
                      set Flag "True"
                  return $Flag }}
    if {$Flag == "False"} {
    #FAil step
    "Retuen Error" 
    }
}

#calling the proc:
set lines [split $OutPut "\n"]
set expected_output "| sc-2 |              | running |"
set Result [check_if_exist_in_output_reg $lines $expected_output] 

但上面的过程总是返回 TRUE,不计量我发送的内容。 虽然我希望得到 False 以防这条线真的不存在。 我也考虑将预期结果发送为:

    set expected_output "| sc-2 |[regexp {(?:\d+\.){3}\d+}]| running |"

但我不确定如何以正确的方式编写它。

【问题讨论】:

  • 你的正则表达式相当困难。有没有^.*sc-2.*running.*$不够用的情况?
  • 嗯,没想到。我想这很好。
  • 感谢它解决并简化了一切
  • 你的代码的问题是你发送给proc的正则表达式不仅仅是一个字符串,它包含正则表达式特殊字符|。因此,您正在测试 $Str 是否包含空字符串或“sc-2”或一系列空格或“运行”或空字符串。而且由于每个字符串都包含空字符串,因此结果始终为真。

标签: tcl regexp-replace


【解决方案1】:

正如 jasonmclose 所建议的那样(我不确定如何在此处标记)。 我已将正则表达式更改为更简单的一个。并且 proc 像这样工作得很好:

    proc check_if_exist_in_output_with_reg_exspression {lines String_to_Check} {
    set err_msg ""
    set Flag "False"
    foreach line $lines {  if {[regexp ^.*sc-2.*running.* $String_to_Check ]==1} {
                          set Flag "True"
                          return $Flag }}
    if {$Flag == "False"} {
    #Fail step
    return "Error"
    }
}

【讨论】:

    【解决方案2】:

    这不是 OP 关于正确使用正则表达式的问题的答案,但值得一提的是,整个 fuzz 也可以在没有正则表达式的情况下解决。你可能要考虑某事。大致如下:

    set data {
    +------+--------------+---------+------------+-----------------------+
    | id   | IP address   | status  | type       | created at            |
    +------+--------------+---------+------------+-----------------------+
    | sc-1 |              | running | r4.2       | Aug 21, 2017 08:09:44 |
    | sc-2 | 164.54.39.30 | running | r4.2       | Aug 21, 2017 08:09:44 |
    +------+--------------+---------+------------+-----------------------+
    }
    
    set status [dict create]
    foreach line [lrange [split [string trim $data] "\n"] 3 end-1] {
        set line [lmap el [split [string trim $line |] |] {string trim $el}]
        dict set status [lindex $line 0] [lindex $line 2]
    }
    
    puts [dict get $status "sc-2"]
    

    【讨论】:

      【解决方案3】:
                  set Output {+------+--------------+---------+------------+-----------------------+
                  | id   | IP address   | status  | type       | created at            |
                  +------+--------------+---------+------------+-----------------------+
                  | sc-1 |              | running | r4.2       | Aug 21, 2017 08:09:44 |
                  | sc-2 | 164.54.39.30 | running | r4.2       | Aug 21, 2017 08:09:44 |
                  +------+--------------+---------+------------+-----------------------+};
      
                  proc check_if_exist_in_output_reg {lines Str} { 
                              set Flag "False"
                              if {[regexp  -linestop $Str $lines a] ==1} { ; #-linestop for matching whithin a line, a for just storing match in a variable a
                                  #puts $a
                                  set Flag "True"
                                  return $Flag 
                              } else {
                                  return "Error"
                              }
                  }
      
                  #calling the proc:
                  #set lines [split $Output "\n"] : not needed
                  set expected_output {sc-1.*running} ; #brackets for literal substitution
                  set Result [check_if_exist_in_output_reg $Output $expected_output] 
                  puts $Result
      

      【讨论】:

        猜你喜欢
        • 2018-05-25
        • 1970-01-01
        • 2018-06-23
        • 2017-05-25
        • 1970-01-01
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 2011-08-26
        相关资源
        最近更新 更多