cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
882
Views
5
Helpful
4
Replies

Regular expression question...

ringwyrm
Level 1
Level 1

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?

4 Replies 4

Jon Marshall
Hall of Fame
Hall of Fame

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
Level 7
Level 7

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.

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

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

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Review Cisco Networking products for a $25 gift card