A regular expression is a mode matching tool. You can create a matching mode based on specified rules and then match target objects based on the matching mode. A regular expression consists of 1 to 256 common characters and special characters.
A regular expression matches a string against the defined common characters. The common characters include all uppercase and lowercase letters, digits, underscores, punctuations, and some special symbols. For example, a matches the letter "a" in "abc", 10 matches the digits "10" in "10.113.25.155", and @ matches the symbol "@" in "xxx@xxx.com".
Special characters are a set of symbols with special meanings which are provided to flexibly create matching modes. The special characters are also called metacharacters. Table 1 describes special characters and their syntax.
Table 1 Description of special characters |
Special Character | Function | Example |
\ | Defines an escape character, It converts a special or common character next to it to a common character. | \* matches the asterisk (*). |
^ | Matches the starting position of the string. | ^10 matches 10.10.10.1 instead of 20.10.10.1. |
$ | Matches the ending position of the string. | 1$ matches 10.10.10.1 instead of 10.10.10.2. |
* | Matches the preceding element zero or more times. | 10* matches 1, 10, 100, 1000, and so on. (10)* matches null, 10, 1010, 101010, and so on. |
+ | Matches the preceding element one or more times. | 10+ matches 10, 100, 1000, and so on. (10)+ matches 10, 1010, 101010, and so on. |
? | Matches the preceding element zero or one time. NOTE: Currently, if you enter the regular expression (?) on Huawei data communications devices, the command help information is displayed. Huawei data communications devices do not support special characters in the regular expression (?). | 10? matches 1 or 10. (10)? matches null or 10. |
. | Matches any single character. | 0.0 matches 0x0, 020, and so on. .oo. matches book, look, tool, and so on. |
() | Defines a subexpression, which can be null. Both the expression and the subexpression should be matched. | 100(200)+ matches 100200, 100200200, and so on. |
x|y | Matches x or y. | 100|200 matches 100 or 200. 1(2|3)4 matches 124 or 134, rather than 1234, 14, 1224, and 1334. |
[xyz] | Matches any single character in the regular expression. | [123] matches the character 2 in 255. |
[^xyz] | Matches any character that is not in the regular expression. | [^123] matches any character except 1, 2, and 3. |
[a-z] | Matches any character within the specified range. | [0-9] matches any character ranging from 0 to 9. |
[^a-z] | Matches any character beyond the specified range. | [^0-9] matches all non-numeric characters. |
A simple regular expression does not contain any special character. For example, you can create a simple regular expression "hello" to match the character string "hello" only. In practice, multiple common and special characters are used together to match a character string with special features.
Certain special characters degenerate to common characters when they are placed at certain positions in a regular expression.
The special characters following "\" match special characters themselves.
The special characters "*", "+", and "?" are placed at the starting position of the regular expression. For example, +45 matches +45 and abc(*def) matches abc*def.
The special character "^" is placed at any position except the start of the regular expression. For example, abc^ matches abc^.
The special character "$" is placed at any position except the end of the regular expression. For example, 12$2 matches 12$2.
A right parenthesis ")" or right bracket "]" is not paired with a corresponding left parenthesis "(" or bracket "[". For example, abc) matches abc) and 0-9] matches 0-9].