📄 regex
字号:
============================================================================== Regex Guide============================================================================== This is a small summary of most of the regex operators and how to use them. \ Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) $ Match the end of the line | Alternation (or) [abc] Match only a, b or c [^abc] Match anything except a, b and c * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match at least n but not more than m times \w Match a "word" character (alphanumeric plus "_") \W Match a non-word character \s Match a whitespace character \S Match a non-whitespace character You compose regular expressions from operators. Most operators have more thanone representation as characters.====================1. Operator Overview==================== Match-self Operator Ordinary characters. Match-any-character Operator . Concatenation Operator Juxtaposition. Repetition Operators * + ? {} Alternation Operator | List Operators [...] [^...] Grouping Operators (...) Back-reference Operator \digit Anchoring Operators ^ $The Match-self Operator (ORDINARY CHARACTER)============================================ This operator matches the character itself. All ordinary charactersrepresent this operator. For example, `f' is always an ordinary character, sothe regular expression `f' matches only the string `f'. In particular, itdoes *not* match the string `ff'.The Match-any-character Operator (`.')====================================== This operator matches any single printing or nonprinting characterexcept it won't match a newline. The `.' (period) character represents this operator. For example,`a.b' matches any three-character string beginning with `a' and endingwith `b'.The Concatenation Operator========================== This operator concatenates two regular expressions A and B. No characterrepresents this operator; you simply put B after A. The result is a regularexpression that will match a string if A matches its first part and B matchesthe rest. For example, `xy' (two match-self operators) matches `xy'.Repetition Operators==================== Repetition operators repeat the preceding regular expression a specifiednumber of times. Match-zero-or-more * Match-one-or-more + Match-zero-or-one ? Interval {} The Match-zero-or-more Operator (`*') ------------------------------------- This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o's. Since this operator operates on the smallest preceding regular expression, `fo*' has a repeating `o', not a repeating `fo'. So, `fo*' matches `f', `fo', `foo', and so on. Since the match-zero-or-more operator is a suffix operator, it may be useless as such when no regular expression precedes it. This is the case when it: * is first in a regular expression, or * follows a match-beginning-of-line, open-group, or alternation operator. The matcher processes a match-zero-or-more operator by first matching as many repetitions of the smallest preceding regular expression as it can. Then it continues to match the rest of the pattern. If it can't match the rest of the pattern, it backtracks (as many times as necessary), each time discarding one of the matches until it can either match the entire pattern or be certain that it cannot get a match. For example, when matching `ca*ar' against `caaar', the matcher first matches all three `a's of the string with the `a*' of the regular expression. However, it cannot then match the final `ar' of the regular expression against the final `r' of the string. So it backtracks, discarding the match of the last `a' in the string. It can then match the remaining `ar'. The Match-one-or-more Operator (`+') ------------------------------------ This operator is similar to the match-zero-or-more operator except that it repeats the preceding regular expression at least once. For example, `ca+r' matches, e.g., `car' and `caaaar', but not `cr'. The Match-zero-or-one Operator (`?') ------------------------------------ This operator is similar to the match-zero-or-more operator except that it repeats the preceding regular expression once or not at all. For example, `ca?r' matches both `car' and `cr', but nothing else. Interval Operators (`{' ... `}' ------------------------------- `{COUNT}' matches exactly COUNT occurrences of the preceding regular expression. `{MIN,}' matches MIN or more occurrences of the preceding regular expression. `{MIN, MAX}' matches at least MIN but no more than MAX occurrences of the preceding regular expression. The interval expression (but not necessarily the regular expression that contains it) is invalid if: * MIN is greater than MAX, or * any of COUNT, MIN, or MAX are outside the range zero to `RE_DUP_MAX' (which symbol `regex.h' defines).The Alternation Operator (`|')============================== Alternatives match one of a choice of regular expressions: if you put thecharacter(s) representing the alternation operator between any two regularexpressions A and B, the result matches the union of the strings that A and Bmatch. For example, supposing that `|' is the alternation operator, then`foo|bar|quux' would match any of `foo', `bar' or `quux'. The alternation operator operates on the *largest* possible surroundingregular expressions. (Put another way, it has the lowest precedence of anyregular expression operator.) Thus, the only way you can delimit its argumentsis to use grouping. For example, if `(' and `)' are the open and close-groupoperators, then `fo(o|b)ar' would match either `fooar' or `fobar'. (`foo|bar'would match `foo' or `bar'.) The matcher usually tries all combinations of alternatives so as to matchthe longest possible string. For example, when matching`(fooq|foo)*(qbarquux|bar)' against `fooqbarquux', it cannot take, say, thefirst ("depth-first") combination it could match, since then it would becontent to match just `fooqbar'.List Operators (`[' ... `]')============================ "Lists", also called "bracket expressions", are a set of one or more items.An "item" is a character, a character class expression, or a range expression.The syntax bits affect which kinds of items you can put in a list. We explainthe last two items in subsections below. Empty lists are invalid. A "matching list" matches a single character represented by one of the listitems. You form a matching list by enclosing one or more items within an"open-matching-list operator" (represented by `[') and a "close-list operator"(represented by `]'). For example, `[ab]' matches either `a' or `b'. `[ad]*' matches the emptystring and any string composed of just `a's and `d's in any order. Regexconsiders invalid a regular expression with a `[' but no matching `]'. "Nonmatching lists" are similar to matching lists except that they match asingle character *not* represented by one of the list items. You use an"open-nonmatching-list operator" (represented by `[^'(1)) instead of anopen-matching-list operator to start a nonmatching list. For example, `[^ab]' matches any character except `a' or `b'. Most characters lose any special meaning inside a list. The specialcharacters inside a list follow.`]' ends the list if it's not the first list item. So, if you want to make the `]' character a list item, you must put it first.`[:' represents the open-character-class operator if what follows is a valid character class expression.`:]' represents the close-character-class operator if what precedes it is an open-character-class operator followed by a valid character class name.`-' represents the range operator if it's not first or last in a list or the ending point of a range.All other characters are ordinary. For example, `[.*]' matches `.' and`*'. Character Class [:class:] Range start-end ---------- Footnotes ---------- (1) Regex therefore doesn't consider the `^' to be the firstcharacter in the list. If you put a `^' character first in (what youthink is) a matching list, you'll turn it into a nonmatching list. Character Class Operators (`[:' ... `:]') -----------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -