Hi there!
This post will introduce the AS path filter and regular expressions, as part of the BGP Fundamentals on the Forum. Please check below for more details.
1. About Regular Expressions
A regular expression is a pattern used to match against an input string. It is a widely used tool. Regular expressions are often used in Internet access. For example, when registering at a site, you need to enter your mobile phone number (if requested). How does the background program determine whether the entered mobile phone number is a valid number? In this case, regular expressions can be used for matching. Therefore, regular expressions are very powerful.
In BGP routing policy deployment, regular expressions can also be flexibly used to obtain routes. A BGP route contains the AS_Path attribute. On a complex large-scale network, the AS_Path attribute is also complex. In specific requirements, regular expressions can be used to obtain routes with a specific AS number or AS_Path attribute.
First, let's look at the composition of a regular expression:
![]()
Generally, a regular expression can
contain two types of characters: regular characters and control characters.
Control
characters may be atomic characters, multiplicative characters, or range
characters.
For
example, the regular expression 100 can match any string that contains the
number 100, such as 100 200, 10000, and 21412100. If you want a more precise
match result, use control characters to further control the match result.
The
following describes the characters that may be used in regular expressions.
Atomic characters
. | Match any character (including a space). |
^ | Assert start of string. |
$ | Assert end of string. |
_ | Match any separator, such as ^, $, space, tab, comma (,), and {,}. |
| | Separate alternative patterns. |
\ | General escape character, which converts the following control characters into regular characters. |
Examples of atomic characters
^a.$ | Match the string that starts with a and ends with any single character, such as a0 and a!. |
^100_ | Match 100, 100 200, 100 300 400, and so on. |
^100$ | Match 100. |
100$|400$ | Match 100, 1400, 300 400, and so on. |
^\(65000\)$ | Match only 65000. |
Multiplicative characters
* | 0 or more quantifiers |
+ | 1 or more quantifiers |
? | 0 or 1 quantifier |
Examples of multiplicative characters
abc*d | Match abd, abcd, abccd, abcccd, and so on. |
abc+d | Match abcd, abccd, abcccd, and so on. |
abc?d | Match abd, abcd, abcdefg, and so on. |
a(bc)?d | Match ad, abcd, aaabcd, and so on. |
A multiplicative
character can be applied to a single character or multiple characters. If it is
applied to multiple characters, the characters must be enclosed in parentheses
().
Range characters
[] | Specify a range that contains any of the characters that are enclosed in [ ]. Note another use for the ^ symbol. As an example, the following regular expression is a range that matches any character as long as it is not the letters a, b, c, or d: [^abcd]. With the underscore character (_), you can specify that a pattern exist anywhere in the input string. |
Examples of range characters
[abcd] | Match a, b, c, or d. |
[a-c 1-2]$ | Match a, a1, 62, 1b, xv2, and so on. |
[^act]$ | Match any string that does not end with a, c, or t. |
[123].[7-9] | Match 15 220 and 91 70. |
2. About AS Path Filters
![]()
The AS_Path attribute can be used as a character string and used together with a regular expression to match routes. For example, the AS_Path attribute 123 495 602 is a character string in which three numbers are separated by two spaces. You can run the as-path-filtercommand and specify a regular expression to match the AS_Path attribute of BGP routes and find the matched routes to enforce a routing policy.
![]()
The preceding figure is an example: The ip as-path-filter 1 permit 443 command defines AS path filter 1, using the regular expression 443, which matches any string containing 443.
Note that
if the AS_Path attribute contains multiple AS numbers, the AS numbers are
separated by spaces, and the spaces can be matched by regular expressions. The
following shows some examples:
^$ | Match the routes whose AS_Path attribute does not contain any AS number, that is, routes within the local AS. |
.* | Match any route. |
^100$ | Match the routes whose AS_Path attribute is 100. |
_100$ | Match the routes whose AS_Path attribute ends with 100, that is, routes originated by AS 100. |
^10[012349]$ | Match the routes whose AS_Path attribute is 100, 101, 102, 103, 104, or 109. |
^10[^0-6]$ | Match the routes whose AS_Path attribute is not 100, 101, 102, 103, 104, 105, or 106. |
^10. | Match the routes whose AS_Path attribute is 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, or 10 because . also matches a space. |
^(100|200)$ | Match the routes whose AS_Path attribute contains 100 or 200. |
12(_34)?_56 | Match the routes whose AS_Path attribute is 12 56 or 12 34 56. |
Note that the ip as-path-filtercommand rejects all routes by default.
3. Examples of AS Path Filters
![]()
In application 1, an AS path filter is defined and associated with a regular expression ^100$. This filter can match the routes whose AS_Path attribute is 100 (excluding any other AS numbers or numbers). Then, the filter is applied to the peer command, which allows only the routes matching the as-path-filter 1 to be transmitted to the BGP peer 10.1.34.4.
In application 2, the if-matchcommand in a route-policy is used to invoke the defined AS path filter, and then the apply command is used to set the attribute value of the LP path. In the BGP configuration mode, the route-policy is applied to the peercommand (in the import direction). In this way, after a router receives the BGP routes from the BGP peer 10.1.34.4, the router sets the LP path attribute to 100 for all the routes that match the AS path filter.
· Example 1 of an AS path filter
![]()
· Example 2 of an AS path filter
![]()



