|
|
This appendix explains how to use regular expressions and contains the following sections:
The components of a regular expression are:
A branch is zero or more concatenated pieces. It matches a match for the first, followed by a match for the second, and so on.
A piece is an atom possibly followed by asterisk (*), plus (+), or question mark (?).
An atom is:
\t, \n, \b, \r, and \f represent the characters: tab, newline, backspace, carriage return, and form feed.
FRED*
For example, the following wildcard matches only "FRED":
FRED
To emulate a wildcard search, the expression must match any character. For this, regular expressions use the period (.) character. For example the following regular expression matches FRED followed by any single character:
FRED.
However, this is still not equivalent to the wildcard search. The asterisk (*) in the wildcard search can match with no characters or any number or characters. This is actually applicable in regular expressions to a point. For example, the following regular expression does exactly the same as the wildcard pattern above:
FRED.*
However, in a regular expression, when you use an asterisk (*), it means none, one, or more of the previous character in the pattern.
In the example, the asterisk is preceded with a period, which means none, one, or more of any character.
Table D-1 shows a comparison of matches.
| String Comparison | FRED.* | FRED.+ | FRED. | FRED... |
FREDRICK | yes | yes | no | no |
FRED | yes | no | no | no |
FRED123 | yes | yes | no | yes |
FRED AND BILL | yes | yes | no | no |
BEFORE FRED | no | no | no | no |
You can use an asterisk and plus after any characters and follow it with more text. Table D-2 shows some examples of this, and some of the other active characters in regular expressions.
| Example | Description |
START *END | Matches a string that starts with the text "START" followed by any number of spaces (or none), followed by the text "END". |
A*B* | Matches any number of A's followed by any number of B's. |
(Testing)+ | Matches any string made up of the word "Testing", for example, "TestingTesting". |
START([0-9])+END | Matches a string that starts with the text "START" followed by one or more numbers 0-9, followed by the text "END". |
(Testing)+
However, the following expression looks for the string "Testin" followed by one or more of the letter "g".
Testing+
For example, the following expression matches with a, b, c, or d:
[abcd]
The following expression matches with a, b, c, or thing:
[abc(thing)]
[^abcd]
The caret character has another use; when used in a regular expression outside the brackets [], it acts as a marker for start of line. Therefore, the following regular expression only matches the string "blah" at the start of a line:
^blah
The caret is often used in conjunction with a dollar sign ($) which is an end of line marker. For example, the following expression only matches a string just containing "blah":
^blah$
The last active character is the pipe character (|). This is an or operation. Following is an example expression to match "abcd" or "fghi":
abcd|fghi
To use any of these active characters as something to match against, you must escape them with a back slash (\). For example, when you want to match with an opening bracket followed by any digits or spaces, then followed by a close bracket, you could use the following regular expression:
\[[0-9]*\]
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Posted: Mon Sep 27 17:57:42 PDT 1999
Copyright 1989-1999©Cisco Systems Inc.