问题
Normally, switch case in expect looks like this:
switch -- $count \
1 {
set type byte
} 2 {
set type word
} big {
set type array
} default {
puts "$count is not a valid type"
}
I need to use regex operators such as | or & - how can I do that?
The below example does not work with expect code :
switch -- $variable \
("a"|"b"|"c") {
do something
}
("a"|"b"|"c") represents a or b or c, but it does not seem to work.
How do I use such statements in switch or may be an and statement?
回答1:
Use the -regexp
option for the command, and brace the expression the Tcl way. Also, you can use braces around all the switches so you don't have to use line continuations.
switch -regexp -- $variable {
{a|b|c} {
do something
}
{^[def].*(?:g|h|i)} {
do a different thing
}
default {
do something else
}
}
http://tcl.tk/man/tcl8.5/TclCmd/switch.htm
http://tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm
来源:https://stackoverflow.com/questions/15242418/how-to-use-regex-in-switch-case-in-expect-script