how to use regex in switch case in expect script?

本小妞迷上赌 提交于 2021-02-08 11:45:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!