Regular expression metacharacters and their meaning

Regular expression metacharacters and their meaning

Metacharacter description
\ Put the next character token, or a backward quote, or an octal escape character. For example, "\n" matches\n. "\N" matches a newline character. The sequence "\" matches "\" and "(" matches "(". It is equivalent to the concept of "escape character" in many programming languages.
^ Match the beginning of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after "\n" or "\r".
$ Match the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before "\n" or "\r".
* Match the preceding sub-expression any number of times. For example, zo* can match "z", as well as "zo" and "zoo". *Equivalent to o{0,}
+ Match the preceding sub-expression one or more times (greater than or equal to 1 time). For example, "zo+" can match "zo" and "zoo", but not "z". +Equivalent to {1,}.
? Matches the preceding subexpression zero or one time. For example, "do(es)?" can match "do" or "do" in "does". ? Equivalent to {0,1}.
{n} n is a non-negative integer. Matches determined n times. For example, "o{2}" cannot match the "o" in "Bob", but it can match the two o's in "food".
{n,} n is a non-negative integer. Match at least n times. For example, "o{2,}" cannot match the "o" in "Bob", but it can match all o in "foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".
{n,m} Both m and n are non-negative integers, where n<=m. Match at least n times and match at most m times. For example, "o{1,3}" will match the first three o's in "fooooood" as a group, and the last three o's as a group. "O{0,1}" is equivalent to "o?". Please note that there can be no spaces between the comma and the two numbers
. Match any single character except "\r\n". To match any character including "\r\n", use a pattern like "[\s\S]".
(pattern) Match the pattern and get this match. The obtained matches can be obtained from the generated Matches collection, the SubMatches collection is used in VBScript, and the $0...$9 properties are used in JScript. To match parenthesis characters, use "(" or ")".
(?:pattern) Non-acquisition matching, matching the pattern but not obtaining the matching result, and not storing it for later use. This is using the or character "(
(?=pattern) Non-acquisition matching, positive positive pre-check, matching the search string at the beginning of any string matching the pattern, the match does not need to be acquired for future use. That is to say, after a match occurs, the search for the next match starts immediately after the last match, rather than after the character that contains the pre-check.
(?!pattern) Non-acquisition matching, forward negative pre-check, matching the search string at the beginning of any string that does not match the pattern, the match does not need to be acquired for future use.
(?<=pattern) Non-acquisition matching, reverse positive pre-check, similar to positive positive pre-check, but in the opposite direction
(?<!pattern) Non-acquisition matching, reverse negative pre-check, similar to positive negative pre-check, but in the opposite direction.
[xyz] Character collection. Match any one character contained. For example, "[abc]" can match the "a" in "plain".
[^xyz] Negative character set. Match any character not included. For example, "[^abc]" can match "plin" in "plain"
[a-z] Character range. Match any character in the specified range. For example, "[az]" can match any lowercase alphabetic character from "a" to "z". Note: Only when the hyphen is inside the character group and appears between two characters, can it represent the range of characters; if the beginning of the character group is out, it can only represent the hyphen itself
\b Match a word boundary, that is, the position between the word and the space (that is, the "match" of regular expressions has two concepts, one is the matching character and the other is the matching position, where \b is the matching position). For example, "er\b" can match the "er" in "never" but not the "er" in "verb".
\B Match a word boundary, that is, the position between the word and the space (that is, the "match" of regular expressions has two concepts, one is the matching character and the other is the matching position, where \b is the matching position). For example, "er\b" can match the "er" in "never" but not the "er" in "verb".
\cx Matches the control character specified by x. For example, \cM matches a Control-M or carriage return character. The value of x must be one of AZ or az. Otherwise, treat c as a literal "c" character.
\d Match a numeric character. Equivalent to [0-9]. grep needs to add -P, perl regular support
\D Match a non-digit character. Equivalent to [^0-9]. Grep needs to add -P, perl regular support
\f Matches a form feed character. Equivalent to \x0c and \cL.
\n Match a newline character. Equivalent to \x0a and \cJ.
\r 匹配一个回车符。等价于\x0d和\cM。
\s 匹配任何不可见字符,包括空格、制表符、换页符等等。等价于[
\f\n\r\t\v]。

Guess you like

Origin blog.csdn.net/Houtieyu/article/details/108716584