Using Regular Expressions

You can use regular expressions when you define AS-path access lists and community lists to filter routes more easily. 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 with the route-map or neighbor filter-list command. If the AS path matches the regular expression in the access list, the route matches the access list.

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 that originate in autonomous system 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 using a route-map command. If the community attribute matches the regular expression in the community list, the route matches the community list.

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–4,294,967,295.

Metacharacters

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

Table 5: 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 zero or more sequences of the immediately previous character or pattern.

+

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

?

Matches zero or one 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 following characters: ^, $,.,*, +,?, (), [ ], – (hyphen).

|

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 an 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 the AS-confed-set or AS-confed-seq type by enclosing that segment with parentheses.

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

The following AS-path access list uses a regular expression to match routes that have 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 that have 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 6 lists some representative regular expressions that you might use in an AS-path access list or community list, along with sample attribute values that match or do not match the regular expression.

Table 6: Sample Regular Expressions

Regular
Expression

Matched AS-Path or Community Attribute

Example

^12

Begins with 12

12 23 4212 629
121245 19

but not
58 12 7

[^12]

Includes any numeral except 1 or 2

44 73 465 69
8

but not
1145 1912
2 49

305$

Ends with 305

89 611 305305
42 30519 1305
6666:305

.5

Includes any one character followed by the numeral 5

89 611 3533 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

179 35 2433 252 129 48
2129 14600:2129
321:94

.*

Includes any character; matches all AS paths and community lists

 

42*

Includes a number that has a numeral 4 followed by zero or more instances of the numeral 2

67 42 51314
33 252 422 483142
4 339 7831422

1(37)*

Includes a sequence that has a numeral 1 followed by zero or more instances of the pattern 37

137 42 211373737 29 4
1

but not
4 3737 78

42+

Includes a number that has a numeral 4 immediately followed by one or more instances of the numeral 2

67 42 2133 252 422 48

but not
4 329 78

1(37)+

Includes a sequence that has a numeral 1 immediately followed by one or more instances of the pattern 37

1373737 29 44 37137 78
137 42 21

but not
4 372 2121 37 5
1 456 881

42?

Includes a number that has a numeral 4 followed by zero or only one instance of the numeral 2

67 42 714 359 78

but not
33 252 422 48

1(37)?

Includes a sequence that has a numeral 1 followed by zero or only one instance of the pattern 37

137 42 2153 612 49
1

but not
4 13737 78

7..

Includes a sequence of three characters, where the first character is numeral 7

600 700 10025 7771

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

^7..

Includes a number in the range 700–799

6127 723 999 700 100 600

but not
25 7771307 800

^7..$

Consists only of a number in the range 700–799

723 700

but not
25 7771307 800
6127 723 999700 100 600

[621]

Includes any of the numerals 6, 2, or 1

60 4334 545 92
200710
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)

(22 431) 102(22 431) 55 76

but not
43 (22 431) 522 431 59

{41 19}

(AS-path attribute only) Includes the AS-set or AS-seq {41 19}

{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

43 101 102 5103 105 22

but not
19 102 101102 103

_200_

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

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.

33 200 422 48^200$
^200 500$

but not
33 20 422 48 51 2005

Related Documentation