Regular Expression Examples and Applications: --------------------------------------------- URLs: ----- http://etext.lib.virginia.edu/helpsheets/regex.html http://www.ieng.com/warp/public/459/26.html http://tinlib.ujep.cz/cisco/data/doc/software/10_3/ptcgcr/70929.htm http://www.ianai.net/ispf/BGP-201/BGP-201.PPT http://www.conservatory.com/info/gawk.info.Regexp.html ------------------------------------------------------------------------ Special Symbols Used: . - Matches any single character * - Matches 0 or more sequences of the pattern + - Matches 1 or more sequences of the pattern ? - Matches 0 or 1 occurrences of the pattern ^ - Matches the beginning of the input string $ - Matches the end of the input string _ - Matches a comma, left brace, right brace, left parenthesis right parenthesis, the beginning of the input string, or a space. \char - Used to dis-associate "special" characters with their special properties and make them normal characters. If you need to use one of the following "special" symbols, simply insert a backslash in front of the character: \. \* \+ \? If you want to specify a range of matches, such as: a, e, i, o, or u, it can be done using brackets, []. When using brackets, only one character must match in order for the regex to be true. [aeiou] - regex to match anything with a vowel You can also include ranges by using dashes.. [a-dA-D] - regex to match any char a-d, upper or lowercase. [a-dA-D\-] - regex to match any char a-d, upper or lowercase. Also matches for a hyphen. [a-dA-D\-\]] - regex to match any char a-d, upper or lowercase. Also matches for a hyphen, or close bracket. You can reverse the use of brackets by inserting a ^ character. [^a-dqsv] - This example matches any letter -except- the ones listed [^\]d] - This example matches any letter -except- ] and d. Using Multipliers: You can create more complex regular expressions that instruct the IOS to match multiple occurrences of a specified regular expression. There are special characters that may be used to be used as Multipliers. * - Matches 0 or more single- or multiple-char patterns + - Matches 1 or more single- or multiple-char patterns ? - Matches 0 or 1 occurrences of the single- or multiple-char patterns a* - Matches any number of occurrences of the letter, a, including none. a+ - Requires there to be at least one letter a in the string to be matched. ba?b - Matches the string "bb" or "bab" \** - Matches any number of asterisks. To use multipliers with multiple-character patterns, enclose the pattern in parentheses. In the following example, the pattern matches any number of the multiple-character string "ab": (ab)* As a more complex example, the following pattern matches one or more instances of alphanumeric pairs (but not none; that is, an empty string is not a match): ([A-Za-z][0-9])+ Alternation: Alternation allows you to specify alternative patterns to match against a string. You separate the alternative patterns with a veritcal bar (|). Exact one of the alternatives can match the input string. codex|telebit - Matches either "codex" or "telebit" Anchoring: You can instruct the IOS software to match a regular expression pattern against the beginning or the end of the input string. That is, you can specify that the beginning or end of an input string contain a specific pattern. You "anchor" these regular expressions to a portion of the input string using the special characters shown below: ^ - Matches the beginning of the input string $ - matches the end of the input string ^abcd - Matches an input string only if the string starts with "abcd" [^abcd] - A range that matches any single letter, as long as it is not the letters, a, b, c, or d. \.12$ - Matches an input string that ends with ".12" BGP AS-Path Examples: --------------------- ^123_ This regular expression allows any announcement as long as its AS-Path begins with AS 123. This is not very secure since an announcement can originate from any AS that is behind AS 123. Matches the following: 123 123 321 123 123 321 123 321 456 981 ... _123$ This regular expression matches any AS-Path that ends in AS 123. Matches the following: 123 100 123 100 200 123 100 200 123 123 ... ^(123_)+$ This regular expression allows for prepending an AS, but -only- allows announcements from the specified AS, in this case AS 123. Matches the following: 123 123 123 123 123 123 123 123 123 123 ... ^.*(_123)+$ This regular expression matches anything with 1 or more occurrences of the AS 123. Matches the following: 123 100 123 100 200 123 123 100 200 300 123 123 123 123 ... ^(123)(_123)*$ This regular expression matches any value that BEGINS with 123. It will also match any # of occurrences of " 123" following the initial value of 123, as well as 0 occurrences. This is also used for permitting pre-pended AS #s. Matches the following: 123 123 123 123 123 123 123 123 123 123 ... AS-Path ACL examples: ! ip as-path access-list 1 seq 5 permit ^123 ! ip as-path access-list 2 seq 5 permit ^(_123)+$ ! ip as-path access-list 3 seq 5 permit ^(123)(_123)*$ ! ----------------------------------------------------------------------------- This file was written by: Eric Cables This file was last modified on: 10/21/2000