Cisco – How to use “Pattern Recall” pattern matching in Regex

cisco

I'd like to match the 0's in this output, specifically using "Pattern Recall" feature mentioned in Cisco documentation.

The pattern is:

Gi4/3                       0             0             0             0
Gi4/4                       0             0             0             0
Gi4/5                       0             0             0             0
Gi4/6                       0             0             0             0

I'm trying to get this to work, but it gives no output:

switch#show int count | i (0 +)\\1\\2\\3\\4

According to this Cisco Doc it should work,

To create a regular expression that recalls a previous pattern, use parentheses to indicate memory of a specific pattern and a double backslash (\) followed by a digit to reuse the remembered pattern. The digit specifies the occurrence of a parenthesis in the regular expression pattern. When there is more than one remembered pattern in the regular expression, \1 indicates the first remembered pattern, \2 indicates the second remembered pattern, and so on.

Best Answer

There are a few things that make this fail:

  1. This is trying to match the 0 five times. The first time you write the expression it matches, then it tries to match four more times,
  2. This is matching 0 (with a trailing space) The last 0 won't have a trailing space,
  3. This expression is incrementing the remembered pattern recall number, even though you only have one pattern,
  4. The pattern recall only needs one backslash. \\1 would match the text "\1". The firstbackslash is the escape character, the character after that is the escaped character.
  5. Technically, with the trailing spaces, this would also match 10 0 0 0 but there is little chance of that being an output. There will never be a number beginning in 0 though.

So, to get the pattern you want, this will work:

#show interfaces stats  | i ( +0)\1\1\1

In this expression, you match any amount of spaces ending with a 0, repeated 3 additional times.

As a nice example of how pattern recall works. (+ 0)(+ 0)\1\2 works too. To make this more clear ( +0)( +1)\1\2\2 would match 0 1 0 1 1

Related Topic