有这个非常简单的脚本:
#!/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 添加到 regexp 返回 2
所以我确定它匹配两个值,但我不知道为什么它只传递第一个匹配的,我也是删除"#host=CE101
,成功匹配CE102
最佳答案
您只是缺少 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
关于Expect 脚本中的正则表达式仅传递第一个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23780471/