Note that there are some explanatory texts on larger screens.

plurals
  1. POTcl/Expect regular expression - want to make lazy (as opposed to greedy)
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 </code></pre> <p>Another possible output:</p> <pre><code>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 </code></pre> <p>My expect statement, using regexp, is the following:</p> <pre><code>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" } </code></pre> <p>(The 3 backslashes are so that they get through Tcl parsing, and a * is handed to the regexp interpreter, to match a literal asterisk.)</p> <p>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:</p> <pre><code>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&gt;" (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" </code></pre> <p>I would like the match to stop at the FIRST \r\n.</p> <p>So, for non-greedy match, I would have thought I needed to add an "?" as follows:</p> <pre><code>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" } </code></pre> <p>Problem is, this does not seem to work. I get the following from the debug output:</p> <pre><code>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) </code></pre> <p>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.</p> <p>Thanks, James</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload