Regular expression question...

Unanswered Question
Apr 23rd, 2010
User Badges:

So, you can attach a multiplier to a multicharacter sequence like so:


(ab)+ 


That mean one or more occurences of the sequence "ab"


But what if I want to match on a string that does NOT have this sequence?  Like I want to match on a static route in a configuration that does not contain a tag...


"ip route .* [^(tag)]"



But that doesn't work... how do you specify that a string that does not contain a multi-character sequence?

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (1 ratings)
Loading.
Jon Marshall Sat, 04/24/2010 - 01:55
User Badges:
  • Super Blue, 32500 points or more
  • Hall of Fame,

    Founding Member

  • Cisco Designated VIP,

    2017 LAN, WAN

ringwyrm wrote:


So, you can attach a multiplier to a multicharacter sequence like so:


(ab)+ 


That mean one or more occurences of the sequence "ab"


But what if I want to match on a string that does NOT have this sequence?  Like I want to match on a static route in a configuration that does not contain a tag...


"ip route .* [^(tag)]"



But that doesn't work... how do you specify that a string that does not contain a multi-character sequence?


Unfortunately the regular expression support in IOS is not like that of a scripting language like Perl/TCL. It is only a limited subset and there is no negation charater in that subset as far as i know.


Jon

marikakis Sat, 04/24/2010 - 15:43
User Badges:
  • Gold, 750 points or more

According to documentation: You can reverse the matching of the range by including a caret (^) sign at the start of the range...The following example matches anything except a right square bracket (]) or the letter d: [^\]d]



So, maybe there is a problem with the meaning of the expression you have within brackets (i.e. the "^(tag)"). I am not sure exactly what this means in the context of a range enclosed within brackets. If you had [^tag] it would match anything except t or a or g. So, maybe could you try the following:



"ip route .*[^t][^a][^g].*"



After examining the syntax of the ip route command, I wonder whether you could have a "g" anywhere except within the word "tag". The only pitfalls I can think of now are: a name for the route that contains the letter "g" or a route via say some GigabitEthernet. So, maybe you could try the following:



"ip route ([^g])*" (i.e. no "g" anywhere)


or


"ip route (.*[^g]_)* (i.e. no "g" at the end of a word)



I must say I am not sure if any of the above will work, but it probably wouldn't hurt to try them out. Please let us know if any of those or something else worked for you.

Jon Marshall Sat, 04/24/2010 - 15:56
User Badges:
  • Super Blue, 32500 points or more
  • Hall of Fame,

    Founding Member

  • Cisco Designated VIP,

    2017 LAN, WAN

Maria


Oops, i stand corrected. I though ^ could only be used as a start of line delimeter and not to negate with Cisco IOS. Thanks for correcting.


To the OP, apologies for the misleading information.


Jon

marikakis Sat, 04/24/2010 - 16:13
User Badges:
  • Gold, 750 points or more

Don't be in a hurry my friend. I was about to edit my previous post!


After some thinking, the first suggestion ("ip route .*[^t][^a][^g].*") will probably not work, because it could match a route that contains a tag via the surrounding wildcards. A better one might be: "ip route .*[^t][^a][^g]_.*" (i.e. 3 last letters of a word cannot be tag)


As I said previously, I'm not sure about what exactly is going to happen with those suggestions, but maybe we can all learn if author tries them out and provides some feedback.


Edit: Ok, even the suggestion of this post needs additional thinking. Maybe "ip route (.*[^t][^a][^g]_)*" is better. Anyway, I'll wait for feedback before correcting myself again

Actions

This Discussion