📄 regex
字号:
A "character class expression" matches one character from a given class. You form a character class expression by putting a character class name between an "open-character-class operator" (represented by `[:') and a "close-character-class operator" (represented by `:]'). The character class names and their meanings are: `alnum' letters and digits `alpha' letters `blank' system-dependent; for GNU, a space or tab `cntrl' control characters (in the ASCII encoding, code 0177 and codes less than 040) `digit' digits `graph' same as `print' except omits space `lower' lowercase letters `print' printable characters (in the ASCII encoding, space tilde--codes 040 through 0176) `punct' neither control nor alphanumeric characters `space' space, carriage return, newline, vertical tab, and form feed `upper' uppercase letters `xdigit' hexadecimal digits: `0'-`9', `a'-`f', `A'-`F' These correspond to the definitions in the C library's `<ctype.h>' facility. For example, `[:alpha:]' corresponds to the standard facility `isalpha'. Regex recognizes character class expressions only inside of lists; so `[[:alpha:]]' matches any letter, but `[:alpha:]' outside of a bracket expression and not followed by a repetition operator matches just itself. The Range Operator (`-') ------------------------ Regex recognizes "range expressions" inside a list. They represent those characters that fall between two elements in the current collating sequence. You form a range expression by putting a "range operator" between two characters.(1) `-' represents the range operator. For example, `a-f' within a list represents all the characters from `a' through `f' inclusively. Since `-' represents the range operator, if you want to make a `-' character itself a list item, you must do one of the following: * Put the `-' either first or last in the list. * Include a range whose starting point collates strictly lower than `-' and whose ending point collates equal or higher. Unless a range is the first item in a list, a `-' can't be its starting point, but *can* be its ending point. That is because Regex considers `-' to be the range operator unless it is preceded by another `-'. For example, in the ASCII encoding, `)', `*', `+', `,', `-', `.', and `/' are contiguous characters in the collating sequence. You might think that `[)-+--/]' has two ranges: `)-+' and `--/'. Rather, it has the ranges `)-+' and `+--', plus the character `/', so it matches, e.g., `,', not `.'. * Put a range whose starting point is `-' first in the list. For example, `[-a-z]' matches a lowercase letter or a hyphen (in English, in ASCII). ---------- Footnotes ---------- (1) You can't use a character class for the starting or ending point of a range, since a character class is not a single character.Grouping Operators (`(' ... `)')================================ A "group", also known as a "subexpression", consists of an "open-groupoperator", any number of other operators, and a "close-group operator". Regextreats this sequence as a unit, just as mathematics and programming languagestreat a parenthesized expression as a unit. Therefore, using "groups", you can: * delimit the argument(s) to an alternation operator or a repetition operator * keep track of the indices of the substring that matched a given group. for a precise explanation. This lets you: * use the back-reference operator * use registersThe Back-reference Operator ("\"DIGIT)====================================== A back reference matches a specified preceding group. The back referenceoperator is represented by `\DIGIT' anywhere after the end of a regularexpression's DIGIT-th group. DIGIT must be between `1' and `9'. The matcher assigns numbers 1 through 9to the first nine groups it encounters. By using one of `\1' through `\9'after the corresponding group's close-group operator, you can match asubstring identical to the one that the group does. Back references match according to the following (in all examples below, `('represents the open-group, `)' the close-group, `{' the open-interval and `}'the close-interval operator): * If the group matches a substring, the back reference matches an identical substring. For example, `(a)\1' matches `aa' and `(bana)na\1bo\1' matches `bananabanabobana'. Likewise, `(.*)\1' matches any newline-free string that is composed of two identical halves; the `(.*)' matches the first half and the `\1' matches the second half. * If the group matches more than once (as it might if followed by, e.g., a repetition operator), then the back reference matches the substring the group *last* matched. For example, `((a*)b)*\1\2' matches `aabababa'; first group 1 (the outer one) matches `aab' and group 2 (the inner one) matches `aa'. Then group 1 matches `ab' and group 2 matches `a'. So, `\1' matches `ab' and `\2' matches `a'. * If the group doesn't participate in a match, i.e., it is part of an alternative not taken or a repetition operator allows zero repetitions of it, then the back reference makes the whole match fail. For example, `(one()|two())-and-(three\2|four\3)' matches `one-and-three' and `two-and-four', but not `one-and-four' or `two-and-three'. For example, if the pattern matches `one-and-', then its group 2 matches the empty string and its group 3 doesn't participate in the match. So, if it then matches `four', then when it tries to back reference group 3--which it will attempt to do because `\3' follows the `four'--the match will fail because group 3 didn't participate in the match. You can use a back reference as an argument to a repetition operator. Forexample, `(a(b))\2*' matches `a' followed by two or more `b's. Similarly,`(a(b))\2{3}' matches `abbbb'. If there is no preceding DIGIT-th subexpression, the regular expression isinvalid.Anchoring Operators=================== These operators can constrain a pattern to match only at thebeginning or end of the entire string or at the beginning or end of aline. Match-beginning-of-line ^ Match-end-of-line $ The Match-beginning-of-line Operator (`^') ------------------------------------------ This operator can match the empty string either at the beginning of the string or after a newline character. Thus, it is said to "anchor" the pattern to the beginning of a line. In the cases following, `^' represents this operator. (Otherwise, `^' is ordinary.) * It (the `^') is first in the pattern, as in `^foo'. * It follows an open-group or alternation operator, as in `a\(^b\)' and `a\|^b'. The Match-end-of-line Operator (`$') ------------------------------------ This operator can match the empty string either at the end of the string or before a newline character in the string. Thus, it is said to "anchor" the pattern to the end of a line. It is always represented by `$'. For example, `foo$' usually matches, e.g., `foo' and, e.g., the first three characters of `foo\nbar'.============================2. Word and Buffer Operators============================Word Operators Match-word-boundary \b This operator (represented by `\b') matches the empty string at either the beginning or the end of a word. For example, `\brat\b' matches the separate word `rat'. Match-within-word \B This operator (represented by `\B') matches the empty string within a word. For example, `c\Brat\Be' matches `crate', but `dirty \Brat' doesn't match `dirty rat'. Match-beginning-of-word \< This operator (represented by `\<') matches the empty string at the beginning of a word. Match-end-of-word \> This operator (represented by `\>') matches the empty string at the end of a word. Match-word-constituent \w This operator (represented by `\w') matches any word-constituent character. Match-non-word-constituent \W This operator (represented by `\W') matches any character that is not word-constituent.Buffer Operators ("buffers" in mail2sms are the whole input strings that you can match.) Match-beginning-of-buffer \` This operator (represented by `\`') matches the empty string at the beginning of the buffer. Match-end-of-buffer \' This operator (represented by `\'') matches the empty string at the end of the buffer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -