期待正则表达式数字模式

前端之家收集整理的这篇文章主要介绍了期待正则表达式数字模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有期望脚本的问题.我希望在命令输出为“0”时捕获并区别于其他数字或链,如“000”或“100”

我的代码是:

#!/usr/bin/expect -f
set timeout 3
spawn bash send "echo 0\n" 
expect {
-regexp {^0$} { send_user "\nzero\n" }  
-re {\d+} { send_user "\number\n"}
}
send "exit\n"

我有以下回应:

spawn bash
echo 0

number

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

-regexp {^0$} { send_user "\nzero\n" }

如果我改为:

-regexp {0} { send_user "\nzero\n" }

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

send "echo 100\n"
expect {
    -regexp {0} { send_user "\nzero\n" }
    -re {\d+} { send_user "\nnumber\n"}
}

结果:

spawn bash
echo 10

zero

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

更新:

我尝试了建议的修复代码

#!/usr/bin/expect -f
set timeout 3
spawn bash
send "echo 0\n"
expect {
    -regexp {\b0\b} { send_user "\nzero\n" }
    -re {\d+} { send_user "\nnumber\n"}
}
send "exit\n"

但我仍然有这样的回应:

spawn bash
echo 0

number

更新2

我也尝试过这一行:

-regexp {"^0\n$"} { send_user "\nzero\n" }

但我仍然有相同的结果.

提前致谢

解决方法

我可以解决这个问题:

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

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

send "exit\n"

猜你在找的正则表达式相关文章