R12.1 Project-2025Sep08
Basic Regular Expressions
Regular expressions are powerful tools for pattern recognition, including both special and ordinary characters.
- Ordinary characters simply match themselves. For example, A, a, or 0. You can concatenate ordinary characters, so Version matches the string Version.
- Special characters either stand for classes of ordinary characters or affect how the regular expressions around them are parsed. For example: "|", "(".
The following table lists the frequently used special characters.
| Character | Description |
| . | Matches any characters except a newline. |
| ^ | Matches the start of the string or line. |
| $ | Matches the end of the string or just before the newline at the end of the string. |
| * | Matches the previous element zero or more times. For example, "ab*" can match "a", "ab", and "abb". |
| + | Matches the previous element one or more times. For example, "be+" can match "been" and "bent". |
| ? | Matches the previous element zero or one time. For example, "ab?" can match "a" and "ab". |
| *? | Matches the previous element zero or more times, but as few times as possible. For example, "\d*?\.\d" can match "0", "19.9", and "219.6". |
| +? | Matches the previous element one or more times, but as few times as possible. For example, "se+?" can match "see" and "sea". |
| ?? | Matches the previous element zero or one time, but as few times as possible. For example, "rai??n" can match "ran" and "rain". |
| [] | A bracket expression. Matches a single character that is contained within the brackets. For example, [amk] will match "a", "m", or "k". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z]. [0-5][0-9] will match all two-digit numbers from 00 to 59. The - character is treated as a literal character if it is the last or the first character within the brackets. For example, [abc-] and [-abc]. But the backslash escapes are not allowed. |
| | | Matches any one element separated by the vertical bar (|) character. |
| ( ) | Defines a marked sub-expression. The string matched within the parentheses can be recalled later. A marked sub-expression is also called a block or capturing group. |
| \s | Matches any whitespace characters. It is equivalent to the [\t\n\r\f\v] set. |
| \d | Matches any decimal digits. It is equivalent to the [0-9] set. |
| \w | Matches any alphanumeric characters and the underscore. It is equivalent to the [a-zA-Z0-9_] set. |
|
Tip: The CLI command outputs always follow certain patterns. You can use the regular expressions to parse the CLI command outputs and retrieve the useful data. |