PacketLab
BGP · Route Filtering

BGP Regular Expressions

Read AS-path patterns before using them to accept or suppress advertisements.

Read the path string

In an ordinary AS_SEQUENCE displayed as 65020 65030 65040, the left end is the nearest advertised AS and the right end is the origin AS. Regular expressions match that text, not the destination prefix and not the final i ORIGIN code in the table. A read-only regexp query selects rows for display; it does not change routing policy.

Pattern symbols

SymbolInterpretation
^ / $Start / end of the path string.
_AS boundary: start/end, a space, or recognized AS-set/confederation delimiters.
.Any single character; it is not an AS-number wildcard by itself.
* / + / ?Repeat the preceding character or group zero-or-more / one-or-more / zero-or-one times.
[0-9]One digit; [0-9]+ is a nonempty run of digits.
(...) / |Group a pattern / choose alternatives.
\Escape a character that would otherwise be special.

Match common patterns

ExpressionExample matchMeaning
^$Empty pathEmpty only; commonly local-AS origination, including a locally originated route carried over iBGP.
^65020_65020 65030Nearest path token is 65020; longer downstream paths also match.
_65020_65010 65020 6503065020 occurs as a complete AS token anywhere, including either end.
_65020$65010 65020Rightmost origin token is 65020 in a simple AS_SEQUENCE.
^65020$65020Exactly one token; a prepended 65020 65020 will not match.
^65020_[0-9]+$65020 65030Exactly two tokens with the first fixed.
^(65020_)+65030$65020 65020 65030One or more leading copies of 65020, followed by 65030.
^\(65023\)(65023) 65010A displayed confederation sequence starting with member 65023 in parentheses.

Try a read-only query

R1# show ip bgp regexp ^65020_
     Network          Next Hop        Metric LocPrf Weight Path
*>   198.51.100.0/24  192.0.2.2            0             0 65020 i
*>   203.0.113.0/25   192.0.2.2            0             0 65020 65030 i
*>   203.0.113.128/25 192.0.2.2            0             0 65020 65040 i

All three paths start with 65020. Changing the query to an exact single-token expression leaves just the first row.

R1# show ip bgp regexp ^65020$
     Network          Next Hop        Metric LocPrf Weight Path
*>   198.51.100.0/24  192.0.2.2            0             0 65020 i

This is a constructed path dataset, not a live Internet looking-glass capture. The AS Path Filtering lesson applies patterns as actual neighbor policies.

Boundaries and repetition

Searching for 65020 without boundaries also matches digits embedded in a longer AS number such as 165020. Underscores avoid that error. A token appearing anywhere does not prove it is strictly an intermediate transit AS: it might be the first or last token.

Use the current ASN display format when writing patterns. An asplain four-byte ASN is one decimal token; asdot introduces punctuation. AS_SETs and confederation segments require their displayed delimiters to be considered. An AS path describes the advertisement path and is not proof of the packet forwarding path.

Key Takeaways

  • AS-path expressions operate on path text, not destination addresses or ORIGIN codes.
  • Anchors identify the start and end; underscore matches an AS boundary.
  • An empty path commonly represents origination within the local AS.
  • Exact single-token patterns reject prepended repetitions.
  • A show regexp query only changes the displayed rows, not installed policy.

Regular Expressions Quiz

Six questions · four options each · select one answer per question. Submit all answers to see your score and explanations.

1. Which expression matches an empty AS path?
2. What does _65020_ match?
3. Does ^65020$ match 65020 65020?
4. Which end is the origin in an ordinary displayed AS_SEQUENCE?
5. What does show ip bgp regexp change?
6. Why avoid a bare search for digits 65020?
Protocol and command references

AS-path regular expressions · Prefix and access-list route filters · Policy processing order · Router-symbol attribution.