expect - 如何从期望测试 mySQL 响应中解析整数?
问题描述
有谁知道我如何在期望测试中从以下控制台输出中解析整数?
+-----------------+
| static_route_id |
+-----------------+
| 314 |
+-----------------+
我想做的是
proc testRouteId { identity_regex } {
#sign into database (got it)
#fetch routes with matching identity_regex (a column in the database)
send { select static_route_id from static_routes where identity_regex="$identity_regex"; }
send "\r"
#parse the routeId out of the console output somehow
expect {
timeout { send_user "fetchStaticRouteId timed out\n"; return 0 }
eof { send_user "fetchStaticRouteId failed\n"; return 0 }
=========Stuck on the regex =========
-re "REGEX?" { send_user "fetchStaticRouteId $expect_out(1, string)\n" }
}
return routeId; # (an int)
}
解决方案
使用该expect
命令匹配正则表达式模式,捕获由管道和空格包围的数字序列,并从expect_out
数组中提取数字。
在这里,我使用 Tclformat
命令(如 sprintf)使发送字符串更易于使用。您发送命令不会扩展变量,因为您使用大括号 - 请参阅https://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm规则编号 6。
send [format {select static_route_id from static_routes where identity_regex="%s";\r} $identity_regex]
expect -re {\|\s+(\d+)\s+\|.*}
return $expect_out(1,string)
推荐阅读
- typescript - 无法使用 for 循环和 push() 方法创建 Promise 对象列表
- tensorflow2.0 - 使用 HuggingFace 和带有 AutoModel 的 Tensorflow 进行迁移学习不起作用
- swiftui - swiftui tabbar item 图片 头像
- sql - SQL Server:避免舍入错误
- c# - 如何创建 IReadOnlyCollection
返回通用数据的方法 - android - Android如何创建第二个活动屏幕并在另一个显示器(如电视、显示器)中显示?
- python - 将 pytorch 安装到一个 conda 环境会从其他 conda 环境中删除现有安装
- ip - IP地址:2405:4802:352:4d70:f484:e534:d0b6:70ed ? 如何检查?
- jwt - 在 jwt-auth 中刷新页面后如何调用另一个令牌?
- python - 错误:您必须提供至少一项安装要求 - CDSW