[Contents] [Prev] [Next] [Index] [Report an Error]


Using Regular Expressions

You can use regular expressions when defining AS-path access lists and community lists to more easily filter routes. A regular expression uses special characters—often referred to as metacharacters—to define a pattern that is compared with an input string.

AS-path Lists

For an AS-path access list, the input string is the AS path of the routes to which the list is applied via the route-map or neighbor filter-list commands. If the AS path matches the regular expression in the access list, then the route matches the access list.

Example

The following commands apply access list 1 to routes inbound from BGP peer 10.5.5.2. Access list 1 uses a regular expression to deny routes originating in AS 32.

host1(config-router)#neighbor 10.5.5.2 remote-as 32
host1(config-router)#neighbor 10.5.5.2 filter-list 1 in
host1(config-router)#exit
host1(config)#ip as-path access-list 1 deny 32$

Community Lists

For a community list, the input string is the community attribute of the routes to which the list is applied via a route-map command. If the community attribute matches the regular expression in the community list, then the route matches the community list.

Example

The following commands apply route map 5 to routes forwarded to BGP peer 10.5.5.4. Route map 5 uses a regular expression to match community numbers ending with 305, setting the weight of matching routes to 150.

host1(config-router)#neighbor 10.5.5.4 remote-as 425
host1(config-router)#neighbor 10.5.5.4 route-map 5 out
host1(config-router)#exit
host1(config)#route-map 5 permit 10
host1(config-route-map)#match community 305$
host1(config-route-map)#set weight 150

Community Numbers

When you use a regular expression to match a community number, use the appropriate format for the community number in the community list. If you issue the ip bgp-community new-format command, the community number has the format AA:NN where AA is a number that identifies the autonomous system, and NN is a number that identifies the community within the autonomous system. Otherwise, the community number is an integer in the range 1-4294967295.

Metacharacters

Each regular expression consists of one or more metacharacters and zero or more complete or partial AS or community numbers. Table 7 describes the metacharacters supported for regular expression pattern-matching.




Table 7: Supported Regular Expression Metacharacters 
Metacharacter
Description

^

Matches the beginning of the input string.

Alternatively, when used as the first character within brackets—[^ ]—matches any number except the ones specified within the brackets.

$

Matches the end of the input string.

.

Matches any single character, including white space.

*

Matches 0 or more sequences of the immediately previous character or pattern.

+

Matches 1 or more sequences of the immediately previous character or pattern.

?

Matches 0 or 1 sequence of the immediately previous character or pattern.

()

Specifies patterns for multiple use when followed by one of the multiplier metacharacters: asterisk *, plus sign +, or question mark ?

[ ]

Matches any enclosed character; specifies a range of single characters.

- (hyphen)

Used within brackets to specify a range of AS or community numbers.

_ (underscore)

Matches a ^, a $, a comma, a space, a {, or a }. Placed on either side of a string to specify a literal and disallow substring matching. Numerals enclosed by underscores can be preceded or followed by any of the characters listed above.

|

Matches characters on either side of the metacharacter; logical OR.

Using Metacharacters as Literal Tokens

You can remove the special meaning of a metacharacter by preceding it with a backslash (\). Such a construction denotes that the metacharacter is not treated as a metacharacter for that regular expression. It is simply a character or token with no special meaning, just as a numeral has no special meaning. The backslash applies only to the character immediately following it in the regular expression.

On the E-series router, you are likely to use the backslash only for the parentheses characters, ( or ). BGP indicates a segment of an AS path that is of type AS-confed-set or AS-confed-seq by enclosing that segment within parentheses.

Example

The following AS-path access list uses a regular expression to match routes having an AS-path attribute that begins with any AS-confed-set or AS-confed-seq:

host1(config)#ip as-path access-list 1 permit ^\(

The following AS-path access list uses a regular expression to match routes having an AS-path attribute that ends with any AS-confed-set or AS-confed-seq:

host1(config)#ip as-path access-list 1 permit \)$

The following AS-path access list uses a regular expression to match routes having an AS-path attribute that includes the specific AS-confed-set or AS-confed-seq, (100 200):

host1(config)#ip as-path access-list 1 permit \(100 200\)

Regular Expression Examples

Table 8 shows some representative regular expressions that might be used in an AS-path access list or community list, along with sample attribute values that match or do not match the regular expression.




Table 8: Sample Regular Expressions 
Regular Expression
Matches Any AS-path or Community Attribute That...

^12

Begins with 12, such as the following:

12 23 42    12 629 12    1245 19

but not 58 12 7

[^12]

Includes any numeral except 1 or 2, such as the following:

44 73 46    5 69 8

but not 1145 19    12 2 49

305$

Ends with 305, such as the following:

89 611 305    305 42 305    19 1305 6666:305

.5

Includes any one character followed by numeral 5, such as the following:

89 611 35    33 252 12 998 600:500

1.9

Includes a sequence of three characters where the first character is numeral 1 and the third character is numeral 9, such as the following:

179 35 24    33 252 129 48 2129 14    600:2129 321:94

.*

Includes any character; matches all AS paths and community lists

42*

Includes a number having a numeral 4 followed by zero or more instances of numeral 2, such as the following:

67 42 51    314 33 252 422 48    3142 4 339 78    31422

1(37)*

Includes a sequence having a numeral 1 followed by zero or more instances of the pattern 37, such as the following:

137 42 21    1373737 29 4 1

but not 4 3737 78

42+

Includes a number having a numeral 4 immediately followed by one or more instances of numeral 2, such as the following:

67 42 21    33 252 422 48

but not 4 329 78

1(37)+

Includes a sequence having a numeral 1 immediately followed by one or more instances of the pattern 37, such as the following:

1373737 29 4    4 37137 78 137 42 21

but not 4 372 21    21 37 5 1 456 88    1

42?

Includes a number having a numeral 4 followed by zero or only one instance of numeral 2, such as the following:

67 42 71    4 359 78

but not 33 252 422 48

1(37)?

Includes a sequence having a numeral 1 followed by zero or only one instance of the pattern 37, such as the following:

137 42 21    53 612 49 1

but not 4 13737 78

7..

Includes a sequence of three characters where the first character is numeral 7, such as the following:

600 700 100    25 7771

In the following examples, the three characters are 7, space, 8: 307 800     6127 888 999

^7..

Includes a number ranging from 700 to 799, such as the following:

6127 723 999     700 100 600

but not 25 7771    307 800

^7..$

Consists only of a number ranging from 700 to 799, such as the following:

723     700

but not 25 7771    307 800 6127 723 999    700 100 600

[621]

Includes any of the numerals 6, 2, or 1, such as the following:

60 43    34 545 92 200    710 86 53

The regular expression [162] has the same results.

[0-9]

Includes any number in the range 0-9

^\(22 431\)

(AS-path attribute only) Begins with the AS-confed-set or AS-confed-seq (22 431), such as the following:

(22 431) 102    (22 431) 55 76

but not 43 (22 431) 5    22 431 59

{41 19}

(AS-path attribute only) Includes the AS-set or AS-seq {41 19}, such as the following:

{41 19} 53     76 {41 19} 17 255 {41 19}

but not 3 41 19     41 19 532

101 102 | 103 105

Includes either sequence 101 102 or sequence 103 105, such as the following:

43 101 102 5    103 105 22

but not 19 102 101    102 103

_200_

Includes the number 200 (as opposed to the pattern consisting of numeral 2, numeral 0, numeral 0), such as the following:

33 200 422 48    ^200$ ^200 500$

but not 33 20 422 48     51 2005

Our implementation of regular expressions is not literal. Substring matching is enabled by default. Specifying 200 (no underscores) results in a match on 200 and on 2005. The underscore metacharacter disables substring matching.

For information about using AS-path access lists, see Access Lists. For information about using community lists, see Community Lists.


[Contents] [Prev] [Next] [Index] [Report an Error]