New to the site, so bear with me. I'm working on a Tcl/Expect script and trying to match part of the 4th line in the following router output (two possible outputs shown). It will usually have an IP address, but may have a string like in the second sample:
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
Another possible output:
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
My expect statement, using regexp, is the following:
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"
}
(The 3 backslashes are so that they get through Tcl parsing, and a * is handed to the regexp interpreter, to match a literal asterisk.)
My problem is that - not surprisingly - this is doing a "greedy" match, and I need it NOT to be greedy. See debug output, where this is made clear:
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"
I would like the match to stop at the FIRST \r\n.
So, for non-greedy match, I would have thought I needed to add an "?" as follows:
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"
}
Problem is, this does not seem to work. I get the following from the debug output:
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)
I've been staring at this for too long now, so thought I would request some help. Any ideas on what I need to do to get the lazy match I need? Pls note that I'm stuck with using just Basic Regular Expressions on this HP-UX server... Extended Regular Expressions are not available.
Thanks,
James