如何在期望脚本的切换情况下使用正则表达式? [英] how to use regex in switch case in expect script?
问题描述
通常,在 expect 中切换大小写如下:
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")表示a或b或c,但似乎不起作用.
("a"|"b"|"c") represents a or b or c, but it does not seem to work.
如何在switch中使用此类语句,或者可能是and语句?
How do I use such statements in switch or may be an and statement?
推荐答案
对命令使用 -regexp
选项,并以Tcl方式将表达式括起来.另外,您可以在所有开关周围使用花括号,这样就不必使用换行符了.
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
这篇关于如何在期望脚本的切换情况下使用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!