加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

期待正则表达式数字模式

发布时间:2020-12-14 06:08:01 所属栏目:百科 来源:网络整理
导读:我有一个带有期望脚本的问题.我希望在命令输出为“0”时捕获并区别于其他数字或链,如“000”或“100” 我的代码是: #!/usr/bin/expect -fset timeout 3spawn bash send "echo 0n" expect {-regexp {^0$} { send_user "nzeron" } -re {d+} { send_user "
我有一个带有期望脚本的问题.我希望在命令输出为“0”时捕获并区别于其他数字或链,如“000”或“100”

我的代码是:

#!/usr/bin/expect -f
set timeout 3
spawn bash send "echo 0n" 
expect {
-regexp {^0$} { send_user "nzeron" }  
-re {d+} { send_user "numbern"}
}
send "exitn"

我有以下回应:

spawn bash
echo 0

number

但是以下正则表达式不起作用:

-regexp {^0$} { send_user "nzeron" }

如果我改为:

-regexp {0} { send_user "nzeron" }

工作,但它也捕获“00”“10”“1000”等.

send "echo 100n"
expect {
    -regexp {0} { send_user "nzeron" }
    -re {d+} { send_user "nnumbern"}
}

结果:

spawn bash
echo 10

zero

我不知道我做错了什么,我没有在谷歌上找到任何帮助.我也搜索了在这里解决的类似问题,我无法对我的代码进行任何操作.

更新:

我尝试了建议的修复代码:

#!/usr/bin/expect -f
set timeout 3
spawn bash
send "echo 0n"
expect {
    -regexp {b0b} { send_user "nzeron" }
    -re {d+} { send_user "nnumbern"}
}
send "exitn"

但我仍然有这样的回应:

spawn bash
echo 0

number

更新2

我也尝试过这一行:

-regexp {"^0n$"} { send_user "nzeron" }

但我仍然有相同的结果.

提前致谢

解决方法

我可以解决这个问题:

#!/usr/bin/expect -f
set timeout 3

spawn bash
send "echo 0n"
expect -re "(d+)" {
    set result $expect_out(1,string)
}
if { $result == 0 } {
    send_user "nzeron";
} else {
    send_user "nnumbern";
}

send "exitn"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读