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

时间:2016-03-29 16:07:31

标签: regex tcl

在我的正则表达式声明中有人能告诉我我做错了什么吗?它与“可操作性:降级”行不匹配。我试图匹配任何不可操作状态的东西。我是TCL的新手。谢谢!

$ expect_out(缓冲区)的内容它执行正则表达式:

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"
        }
}

输出:(刀片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

2 个答案:

答案 0 :(得分:2)

!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",而且,你必须确保你的输入被视为一行,而是多行,所以。*通配符不会过去\n换行符并弄乱一切。

答案 1 :(得分:2)

@ sweaver2112有正确的答案。我想在混合中添加可维护性:

  • -expanded标志用于其他无意义的空白
  • 使用-line,因此. 匹配换行符(因此“就绪”与“激活状态”位于同一行)
  • -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 ]