Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Expect脚本中的Regex只传递第一个匹配项_Regex_Linux_Bash_Centos_Expect - Fatal编程技术网

Expect脚本中的Regex只传递第一个匹配项

Expect脚本中的Regex只传递第一个匹配项,regex,linux,bash,centos,expect,Regex,Linux,Bash,Centos,Expect,有一个非常简单的脚本: #!/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"

有一个非常简单的脚本:

#!/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

所以我的问题是,regexp只将第一个找到的结果传递给变量,而第二个结果没有被传递。我确信这就是问题所在,因为将-all添加到regexp会返回
2
,因此我确信它匹配两个值,但我不知道为什么它只传递第一个匹配的值,而且我删除了
“#host=CE101
,它成功地匹配了CE102。

您只是缺少了两个匹配的选项


这是对我有效的方法,非常感谢:)
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