BGP Regular Expression (RegEx) Overview

There may be times when conditionally matching on all the networks prefixes may be too complicated, and identifying all routes from a specific organization is preferred, BGP Regular expression (RegEx) is one of the best solution. In such a case, path selection can be made by using a BGP AS_Path.

Regular expressions (regex) are used to parse through a large number of available ASNs (4,294,967,295). Regular expression are based on query modifiers used to select only the match routes with the appropriate content. The BGP table can be parsed with regular expression by using the command show bgp afi safi regexp regex-pattern.

 

BGP Regular Expression

Learning regular expression can take time, but the most common ones used in BGP involve the input string special characters: ^, $, and _.

The table below displays some common BGP regex.

Regular Expression Query Modifiers

ModifierDescription
_ (underscore)Matches a space
^ (caret)Indicates the start of a string
$ (dollar sign)Indicates the end of a string
– (hyphen)Indicates a range of numbers in brackets
[] (brackets)Matches a single character or nesting within a range
[^] (caret in brackets)Excludes the characters listed in brackets
() (parentheses)Used for nesting of search patterns
| (pipe)Provides OR functionality to the query
. (period)Matches a single character, including a space
* (asterisk)Matches zero or more characters or patterns
+ (plus sign)Matches one or more instances of the character or pattern
? (question mark)Matches one or no instances of the character or pattern

\\ is an escape character

You’ll need this for BGP confederations

 

Common BGP RegEx

Learning regex can take time, but the most common ones used in BGP involve the ^, $, and _.

The table below displays some common BGP regex.

Common BGP Regular Expressions

Regular ExpressionMeaning
^$Local originating routes
permit ^200_Only routes from neighbor AS 200
permit _200$Only routes originating from AS 200
permit _200_Only routes that pass through AS 200
permit ^[0-9]+ [0-9]+ [0-9]+?Routes with three or fewer AS_Path entries

Download our Free CCNA Study Guide PDF for complete notes on all the CCNA 200-301 exam topics in one book.

We recommend the Cisco CCNA Gold Bootcamp as your main CCNA training course. It’s the highest rated Cisco course online with an average rating of 4.8 from over 30,000 public reviews and is the gold standard in CCNA training: