Cisco IOS regex behavior — the misunderstood underscore?

Many demonstrations of Cisco IOS regular expression use over-simplify or misstate the actual use and definition of the underscore wildcard _.  This may lead to unexpected behavior in some situations.

In practice, the underscore can often work in an expression as a stand in for “match spaces”, which is typically how it is used in Cisco regular expression examples.   In reality, it can match many other other characters and symbol representations than a space.

Here’s what the documentation says about the underscore:

Matches a comma (,), left brace ({), right brace (}), left parenthesis ( ( ), right parenthesis ( ) ), the beginning of the string, the end of the string, or a space.

(This is from http://www.cisco.com/c/en/us/td/docs/ios-xml/ios/fundamentals/configuration/15_sy/fundamentals-15-sy-book/cf-cli-search.html, but there are numerous Cisco documents with the definition.)

It may be worth testing on multiple Cisco OS platforms and versions, but my use so far been consistent with the documentation.

In IOS, a space inside parentheses with match a space, and in NX-OS, a space inside of a quotation mark will match a space.  IOS behavior with the grouping or delimiting characters (parens) will still work, but spaces represented between words will be the reliable matching construct that can be relied upon on.   NX-OS requires the use of quotation marks to treat multiple words as a group.

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.