14-5 Regular Expressions

  • Regular expressions are templates used to match against patterns in text strings.

  • Regular expressions can be used with modem chat scripts, with BGP access lists, and to search through command output.

Configuration

You can create regular expressions by forming a template string from any of the following components :

  • Match against one or more characters ” Single characters or a set of characters contained within parentheses can be explicitly given for matching.

    In addition, you can use the special characters listed in Table 14-2 to match ambiguous strings.

Table 14-2. Regular-Expression Wildcards
Character Symbol Function
Period . Matches any single character.
Caret ^ Matches starting at the beginning of the string.
Dollar sign $ Matches up to the end of the string.
Underscore _ Matches a comma (,), left brace ({), right brace (}), left parenthesis ((), right parenthesis ()), the beginning of the input string, the end of the input string, or a space.
Backslash \ char Matches the char character ( removes any special pattern matching meaning from the character).
  • Match against a list of characters ” You can place a list of characters within square brackets to match against a single character. For example, you can define a list as [abcxyz], where a character matches if it is a, b, c, x, y, or z.

  • Match against a range of characters ” You can place a range of characters within square brackets to match against a single character. A range is defined as two characters separated by a dash. For example, the range [A-Za-z] matches any character between A and Z or a and z (in other words, any alphabetic character).

  • Match against multiples of a pattern ” You can follow a single character or string of characters within parentheses with one of the characters listed in Table 14-3.

Table 14-3. Regular-Expression Wildcards for Matching Against Pattern Multiples
Character Symbol Function
Asterisk * Matches zero or more sequences of the pattern.
Plus sign + Matches one or more sequences of the pattern.
Question mark ? Matches zero or one occurrence of the pattern.
  • Match against alternative patterns ” Pattern strings separated by a vertical bar () are alternatively used for matching.

Examples

A regular expression is used to find the appropriate modem chat scripts matching Hayes - followed by anything. The period matches any character, and the asterisk uses the period to match zero or more times. Therefore, any character or string after Hayes- is matched.

  line 1   chat-script Hayes-.*  

A regular expression is used to find the appropriate modem chat script for either usrobotics or hayes.

  line 2   chat-script usrobotics   hayes  

A regular expression is used in a BGP AS path list to permit advertisements of a path from the adjacent AS 800. Here, the caret matches the beginning of the AS path string, where 800 is the AS that is adjacent to the local router. The period and asterisk match any other AS path information past the immediately neighboring AS. Also, advertisements of a path through AS 75 are permitted. The underscore characters that surround 75 match any white space or the beginning or end of the AS path string. Refer to Section 7-6 for more information about using regular expressions to match AS paths.

  ip as-path access-list 10 permit ^800 .*   ip as-path access-list 10 permit _75_  

A regular expression is used to search through the list of IPX servers and list only server names beginning with HOSP_.

  show ipx server regexp HOSP\_.+  

A regular expression is used to display only the lines in the running configuration that contain the word VLAN. This might be useful if you need to see a list of the VLAN numbers you have used.

  show running-config   include VLAN.*_  

Suppose you wanted to generate a quick list of interface statistics, consisting of only lines that show the interface number, followed by the line that displays the MAC address. A regular expression is used along with the show interface command to include only the desired information. First, the output lines containing "line protocol" are displayed, because they also begin with the interface type and number.

An alternative pattern is given to display output lines that include the word "Hardware," followed by any string, followed by the word "address." This is done to exclude the "Hardware" line for serial interfaces, which have no MAC addresses.

  show interface   include (line protocol   Hardware.+address)  


Cisco Field Manual[c] Router Configuration
Cisco Field Manual[c] Router Configuration
ISBN: 1587050242
EAN: N/A
Year: 2005
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net