【发布时间】:2013-10-04 03:26:32
【问题描述】:
网站新手,请多多包涵。我正在处理 Tcl/Expect 脚本并尝试匹配以下路由器输出中第 4 行的一部分(显示了两个可能的输出)。它通常有一个 IP 地址,但也可能有第二个示例中的字符串:
Routing entry for 10.1.1.0/30
Known via "static", distance 1, metric 0
Routing Descriptor Blocks:
* 10.3.3.1
Route metric is 0, traffic share count is 1
另一个可能的输出:
Routing entry for 10.1.2.0/24
Known via "static", distance 220, metric 0 (connected)
Advertised by bgp 1234
Routing Descriptor Blocks:
* directly connected, via Null0
Route metric is 0, traffic share count is 1
我的期望语句,使用正则表达式,如下:
expect -re "Routing Descriptor Blocks:\r\n \\\* (.*)\r\n" {
set next_hop $expect_out(1,string)
puts "\n\n*Next-hop address is: $next_hop*\n"
}
(这 3 个反斜杠是为了让它们通过 Tcl 解析,并且将 * 传递给正则表达式解释器,以匹配文字星号。)
我的问题是 - 毫不奇怪 - 这是一个“贪婪”匹配,我需要它不要贪婪。请参阅调试输出,其中清楚地说明了这一点:
expect: does "show ip route 10.1.1.0\r\nRouting entry for 10.1.1.0/30\r\n Known via "static", distance 1, metric 0\r\n Routing Descriptor Blocks:\r\n * 10.3.3.1\r\n Route metric is 0, traffic share count is 1\r\n\r\nRouter>" (spawn_id 4) match regular expression "Routing Descriptor Blocks:\r\n \* (.*)\r\n"? yes
expect: set expect_out(0,string) "Routing Descriptor Blocks:\r\n * 10.3.3.1\r\n Route metric is 0, traffic share count is 1\r\n\r\n"
expect: set expect_out(1,string) "10.3.3.1\r\n Route metric is 0, traffic share count is 1\r\n"
我希望比赛在第一个 \r\n 停止。
所以,对于非贪婪匹配,我会认为我需要添加一个“?”如下:
expect -re "Routing Descriptor Blocks:\r\n \\\* (.*?)\r\n" {
set next_hop $expect_out(1,string)
puts "\n\n*Next-hop address is: $next_hop*\n"
}
问题是,这似乎不起作用。我从调试输出中得到以下信息:
bad regular expression: nested *?+
while executing
"expect -re "Routing Descriptor Blocks:\r\n \\\* (.*?)\r\n" {
set next_hop $expect_out(1,string)
puts "\n\n*Next-hop address is: $next_hop*\n"
}"
(file "./test_telnet_to_router.exp" line 23)
我已经盯着这个太久了,所以我想我会请求一些帮助。关于我需要做些什么来获得我需要的懒惰匹配的任何想法?请注意,我坚持在此 HP-UX 服务器上仅使用基本正则表达式...扩展正则表达式不可用。
谢谢, 詹姆斯
【问题讨论】:
-
这是什么 Tcl 版本? (在交互式期望或 tclsh 会话中,
info patchlevel) -
expect1.1> info patchlevel 7.4 感谢您抽出宝贵时间查看此内容!
标签: regex tcl expect non-greedy