【问题标题】:Regex in Expect script passing only the first matchExpect 脚本中的正则表达式仅传递第一个匹配项
【发布时间】:2014-05-22 09:11:29
【问题描述】:

拥有这个非常简单的脚本:

#!/usr/bin/expect -f


set values "#host=CE101 #host=CE102"
set found [regexp {[A-Z]{1,2}\d{2,3}} $values CE CE1]
if {$found == 1} {
    puts "px is $CE"
    puts "vpx is $CE1"
} else {
   puts "\nfailed to match anything from\r\n$values"
}

puts $found

所以我的问题是正则表达式只将第一个找到的结果传递给变量,而第二个结果没有被传递。我确定这是问题,因为将 -all 添加到正则表达式返回 2 所以我确信它匹配两个值,但我不知道为什么它只传递第一个匹配的值,我也删除了 "#host=CE101 和成功匹配CE102。

【问题讨论】:

    标签: regex linux bash centos expect


    【解决方案1】:

    您只是缺少regexp 的几个选项

    set values "#host=CE101 #host=CE102"
    set re {[A-Z]{1,2}\d{2,3}}
    set hosts [regexp -all -inline $re $values]
    if {[llength $hosts] > 0} {
        foreach host $hosts {puts "found $host"}
    } else {
        puts "no matches"
    }
    
    found CE101
    found CE102
    

    【讨论】:

    • 这是对我有用的方法,非常感谢:)
    猜你喜欢
    • 2023-03-09
    • 2022-01-03
    • 2013-12-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多