BGP Route Filtering Using Regular Expressions

Regular expressions can be used to match patterns in a input string, Regular expressions in BGP is used to filter Autonomous System number from the AS_PATH attribute.

Regular Expression


. Any single character
\ Matches special characters
[] Matches characters or a range of characters separated by a hyphen
^ Beginning of a string
? 0 or 1 of preceding character
$ End of String
* 0 or more preceding character
+ 1 or more of preceding character
| Logical OR
_  Underscore РMatches comma, {} beginning of string, end of string or a space


Examples of Usage:

Matches Prefix originating AS 100


Matches prefix originated and sent from AS 100


Matches prefixes that transited AS 100


Matches all routes that passes through AS 789


^ specifies begin of string

_ next to 789 is a space.

. Match anything after that and also expecting something after that

Match routes with only one AS long or directly connected AS


^ means begin with

[0-9]+ [] means Bracket expression, [0-9] matches any single digit, + means Preceding item must match one or more times meaning it will match 756, 456, 5678 etc

$ The pattern has to appear at the end of a string

Matching prefixes two hops away:


Match repeat AS numbers in the AS-PATH


() – specifies a set, the above expression has two sets. set 1 is ([0-9]+) and second set is (_\1)

[0-9]+ – as example above

\1 – is a special match, that will match anything that is matching set 1 ([0-9]+)

Match repeat AS 100


Match prefixes originating AS 200 or 300 and exactly three hops away


Match routes has no AS in AS-PATH meaning only internal routes (locally originated


Match everything


Using Regular expression in IOS commands Examples:

  ! Matches Prefix originating AS 100
ip as-path access-list 1 deny _100$
! match everything as with ACL a implicit deny apply if the below not used
ip as-path access-list 1 permit .*

router bgp 100
neighbor filter-list 1 in

!Using route-maps

ip as-path access-list 1 deny ^300$
ip as-path access-lis 1 permit .*
route-map FILTER permit 10
match as-path 1
neighbor route-map FILTER OUT