新的網站,所以忍受着我。我正在開發一個Tcl/Expect腳本,並嘗試匹配以下路由器輸出中的第4行的一部分(顯示了兩個可能的輸出)。它通常都會有一個IP地址,但可能有像第二樣品中的字符串:Tcl /期待正則表達式 - 想使懶惰(而不是貪婪)
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"
我想要在FIRST \ 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 感謝您花時間看這個! – James