このサイトは初めてなので、ご容赦ください。私は Tcl/Expect スクリプトに取り組んでおり、次のルーター出力の 4 行目の部分を一致させようとしています (2 つの可能な出力が示されています)。通常は IP アドレスが含まれますが、2 番目のサンプルのような文字列が含まれる場合があります。
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 サーバーでは、基本的な正規表現だけを使用することに固執していることに注意してください... 拡張正規表現は使用できません。
ありがとう、ジェームズ