[c-nsp] cisco regex puzzle of the day

Saku Ytti saku at ytti.fi
Wed Mar 11 13:53:42 EDT 2015


On (2015-03-11 17:28 +0000), Mack McBride wrote:

Hey,

> There is no back tracking in the junos regex nor would backtracking really help.
> Doing this is complicated on cisco due to the lack of negating a full as.

There definitely is backtracking, the reason (64500_)+.+ doesn't work, and
matches 64500 64500 is that after the (64500_)+ has chomped up both of them,
it backtracks, trying to see if by going back, it can further satisfy the .+,
and it'll notice that by going back whole 64500 it can satisfy both.

If it wouldn't backtrack, '64500 64500' wouldn't match, but 64500 64501 would
match, and we would in simple and clear regexp achieve what we want.

However, disabling backtracking globally would break common use-case such as
^.*_64500$

Turns out, some regexp engines allow turning off backtracking conditionally
either by adding '+' after +*, or by adding ?> to group. In which case
(64500_)++.+ and (?>64500_)+.+ would work.
Unfortunately neither regexp engine IOS has supports either of these.


> I haven't tested this but it should work:
> 
> (65400_)+([1-57-9][0-9]*_|6[01-35-9][0-9]*_|64[01-46-9][0-9]*_|645[1-9][0-9]*_|6450[1-9][0-9]*_|64500[0-9]+_)+

Thanks, I was afraid it'll be something terrible, I don't dare to try  :)

-- 
  ++ytti


More information about the cisco-nsp mailing list