BGP Regular Expressions

There can be instances where identifying all the networks from a certain provider may be too complicated or numerous, and a regular expression may be more suitable to filter all networks from a certain AS. Regular expression can be used for this.

A quick guide to regular expression

ModifierPurpose
_Matches a space
^Indicates the start of a string
$Indicates the end of a string
[]Matches a single character with a range of characters
Indicates a range of numbers in brackets
[^]Excludes the characters listed in the brackets
()Nesting of search patterns uses parentheses
|Acts as an OR logic to a query
.Matches a single character including space
*Matches zero or more characters, or pattern
+Matches one or more instances of the character, or pattern
?Matches one or no instances of the character, or pattern

Common regular expression modifiers

Examples of Common Regular Expressions in BGP

^$ – Local originating routes

permit ^205_ – Only routes from neighbor AS 205

permit _205$ – Only routes originating from AS205

permit _205_ – Only routes that pass through AS205

permit ^[0-9]+ [0-9]+ [0-9]+? – Routes with three or fewer AS_Path entries

Some commonly used regular expressions in the context of BGP route manipulation or filtering. Let’s break down their meanings:

  1. .*: Matches anything. This is a wildcard expression that can match any string of characters.
  2. ^$: Matches an empty AS path, indicating locally originated routes. This is useful for filtering routes that originated within the local AS.
  3. ^100_: Matches routes learned from AS 100. This expression specifies that the AS path should begin with AS 100.
  4. _100$: Matches routes originated in AS 100. This expression specifies that the AS path should end with AS 100.
  5. _100_: Matches any instance of AS 100 in the AS path. This expression specifies that AS 100 can appear anywhere in the AS path.
  6. ^[0-9]+$: Matches directly connected ASes. This expression matches AS paths consisting only of digits, indicating directly connected ASes.

Here are some additional useful BGP regular expressions for route manipulation or filtering:

  1. Matching specific AS paths:
    • ^51_65100$ – Matches prefixes originated from AS 65100 directly connected to AS 51.
  2. Matching AS paths with specific AS prepending:
    • ^(51_)+65100$ – Matches prefixes originated from AS 65100 with AS 51 prepended one or more times.
  3. Matching AS paths with a specific AS prepending pattern:
    • ^51_([0-9]+_)+65100$ – Matches prefixes originated from AS 65100 with AS 51 prepending a sequence of AS numbers one or more times.
  4. Matching prefixes from specific AS neighbors:
    • ^([0-9]+)_51_65100$ – Matches prefixes originated from AS 65100, received via AS 51, and originated from one of our directly connected ASes.
  5. Matching prefixes with specific community attributes:
    • ^65100:100$ – Matches prefixes tagged with community 65100:100.
  6. Matching prefixes with specific local preference values:
    • ^_100$ – Matches prefixes with a local preference of 100.
  7. Matching prefixes with specific next-hop addresses:
    • ^192.0.2.1$ – Matches prefixes with the next-hop address 192.0.2.1.