在期望脚本中,如何从字符串变量中删除一组特殊字符?

In an expect script, how do I remove a set of special characters from a string variable?

假设我有一个变量设置为一些用户输入。我无法控制用户输入的内容。

我该如何删除不在 [A-Za-z0-9]、空格、句点或逗号中的所有字符?

proc getUserInput {} {
    set timeout 60
    send_user "\nEnter user input: "
    expect_user {
        -re "(.*)\n" {
            set userInput $expect_out(1,string)
        }
        timeout {
            exitTimeout "Timed out waiting for user input!"
        }
    }
    return $userInput
}

set rawValue [ getUserInput ]
// massage variable goes here?
set massagedValue "$rawValue"

不确定这是否重要,但我使用的是 expect 5.45。

$ expect -v
expect version 5.45

Expect 是一个 Tcl 扩展,因此您可以在编写 Expect 脚本时使用所有 Tcl 命令。您可以在 tclsh:

试试这个
% set v1 "###the string###"
###the string###
% set v2 [regsub -all {[^ .,[:alnum:]]} $v1 ""]
the string
%