如何在 TCL 中进行多个正则表达式测试?

How to do multiple regexp tests in TCL?

谁能告诉我我的 regexp 语句哪里做错了?它与 "Operability: Degraded" 行不匹配。我正在尝试匹配任何不处于可操作状态的东西。我是 TCL 的新手。谢谢!

$expect_out(buffer) 的内容它在以下位置执行正则表达式:

ID 20:
    Location: G1
    Presence: Equipped
    Overall Status: Operable
    Operability: Degraded
    Visibility: Yes
    Product Name: 16GB DDR3-1600-MHz RDIMM/PC3-12800/dual rank/1.35V
    PID: 
    VID: V01
    Vendor: 0x2C00
    Vendor Description: Micron Technology, Inc.
    Vendor Part Number: 
    Vendor Serial (SN): 
    HW Revision: 0
    Form Factor: DIMM
    Type: DDR3
    Capacity (MB): 16384
    Clock: 1600
    Latency: 0.600000
    Width: 64

代码:

proc check_errors { buffer cmd } {
        set count [ regexp -all -- { Activate-Status.*?!Ready|Overall.*Status.*?!Operable|Operability.*?!Operable|Controller.*Status.*?!Optimal|Errors.*?!0|Dr
opped.*?!0|Discarded.*?!0|Bad.*?!0|Suspect.*?!No|Thresholded.*?!0|Visibility.*?!Yes|Thermal.*Status.*?!OK|HA.*?!READY } $buffer ]

        if { [ set count ] != 0 } {
                puts "\tFAIL $cmd (Error Count: $count)"
        } else {
                puts "\tPASS $cmd"
        }
}

输出:(blade 6/5 有一个已知问题,它应该无法通过内存检查)

Blade 6/5 checks...
        PASS show stats
        PASS show version
        PASS show adapter detail
        PASS show cpu detail
        PASS show memory detail
        PASS show inventory detail

!term 并不意味着 "anything but term" 在正则表达式中。对于这种类型的逻辑,您需要 negative lookahead 方法:

Activate-Status(?!.*Ready)|Overall.*Status(?!.*Operable)|Operability(?!.*Operable)|Controller.*Status(?!.*Optimal)|Errors(?!.*0)|Dropped(?!.*0)|Discarded(?!.*0)|Bad(?!.*0)|Suspect(?!.*No)|Thresholded(?!.*0)|Visibility.(?!.*yes)|Thermal.*Status(?!.*OK)|HA.*(?!.*READY)

check it out here

注意:我会使用不区分大小写来过滤掉 "No" 和 "no",而且,您必须确保您的输入是 而不是 被视为单行,但被视为多行,因此 .* 通配符不会超过 \n 换行符并弄乱一切。

@sweaver2112 有正确答案。我想将可维护性添加到组合中:

  • 使用 -expanded 标志来添加无意义的空格
  • 使用 -line 所以 . 匹配换行符(所以 "Ready" 与 [=28= 在同一行])
  • -nocase 用于不区分大小写的匹配(如果这很重要)
    set count [ regexp -all -expanded -line -- { 
        Activate-Status    (?!.*?Ready)    |
        Overall.*Status    (?!.*?Operable) |
        Operability        (?!.*?Operable) |
        Controller.*Status (?!.*?Optimal)  |
        Errors             (?!.*?0)        |
        Dropped            (?!.*?0)        |
        Discarded          (?!.*?0)        |
        Bad                (?!.*?0)        |
        Suspect            (?!.*?No)       |
        Thresholded        (?!.*?0)        |
        Visibility         (?!.*?Yes)      |
        Thermal.*Status    (?!.*?OK)       |
        HA                 (?!.*?READY)
    } $buffer ]