Martin Nikolaev
Martin Nikolaev

Reputation: 233

Regex in Expect script passing only the first match

Having this very simple script:

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

So my problem is that regexp is passing only the first found result to a variable and the second one is not being passed. I am sure that is the problem because of adding -all to regexp is returning 2 so I am sure that its matching both values, but I don't know why it's passing only the first matched, also I deleted "#host=CE101 and it successfully matches CE102.

Upvotes: 1

Views: 291

Answers (1)

glenn jackman
glenn jackman

Reputation: 246837

You're just missing a couple of options for 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

Upvotes: 1

Related Questions