Incorrect pcre and regular expression, or pcre
1. All matching modes in the regular expression should be understood as "matching a character or string and then matching ". This concept is very important.
2. When the parentheses start with delimiters, it indicates that the matching is followed by a character that does not include a given character, rather than a given character.
Most of them are equivalent, but they have different meanings when matching the end of a row. For example, the row matching Aa [^ bcd] $ can be Aaa $ or Aax $, however, only Aa $ is not allowed.
This is the meaning of "matching closely" in the regular expression.
3. (\. [0-9] + )? It can match the decimal point. It cannot be written (\.? [0-9] *), the latter can match the original value after the decimal point even if it cannot match the decimal point.
4. When grouping perl regular brackets, use (? : Replaces the left brackets (, which can indicate that only grouping is not captured. The so-called capture indicates that the variable can be referenced in the reverse direction or saved to the external variable of the regular expression.
([-+]? [0-9] + (\. [0-9] + )?) * (Cm | mm) :( cm | mm) is saved as $3
([-+]? [0-9] + (? : \. [0-9] + )?) * (Cm | mm): (cm | mm) is saved as $2.
5. Special anchor. The Anchor matches the position rather than the character. The same is true for the beginning and end of the line ^.
Note that some programs have different understandings of words and boundary definitions, and some programs do not fully support all of the following special metacharacters.
For example, gnu grep 2.6 does not support \ s and \ d, while gnu grep 2.20 does not support \ s but \ d.
'\ B': Match the null character at the word boundary Match the empty string at the edge of a word.
'\ B': Match the non-null character at the word boundary Match the empty string provided it's not at the edge of a word.
'\ <': Matches an empty character at the beginning of a word to Match the empty string at the beginning of word.
'\>': Matches the empty character at the end of a word to Match the empty string at the end of word.
'\ W': Match word components Match word constituent, it is a synonym for' [_ [: alnum:] '.
'\ W': Match the non-integral part of a word Match non-word constituent, it is a synonym for' [^ _ [: alnum:] '.
'\ S': Match white space, it is a synonym for' [[: space:] '.
'\ S': Match non-blank characters Match non-whitespace, it is a synonym for' [^ [: space:] '.
'\ D': match the number it is a synonym for' [0-9] '.
'\ D': Match non-digital it is a synonym for' [^ 0-9] '.
For example, '\ brat \ B' matches the separate word 'rat ',' \ Brat \ B 'matches 'crate' but not 'furry rat '.
6. character classes. Note that some programs do not fully support all of the following character classes.
'[: Alnum:]': same as '[0-9A-Za-z]'.
'[: Alpha:]': '[: lower:]' and '[: upper:]', same as '[A-Za-z]'.
'[: Lower:]':
'[: Upper:]':
'[: Digit:]': '0 1 2 3 4 5 6 7 8 9 '.
'[: Xdigit:]': Hex digits: '0 1 2 3 4 5 6 7 8 9 a B c d e f a B c d e F '.
'[: Blank:]': space and tab.
'[: Space:]': tab, newline, vertical tab, form feed, carriage return, and space.
'[: Punct:]': Punctuation characters; this is '! "# $ % & '() * +,-./:; <=>? @ [\] ^ _ '{| }~ '.
'[: Print:]': '[: alnum:]', '[: punct:]', and space.
'[: Graph:]': Graphical characters: '[: alnum:]' and '[: punct:]'.
'[: Cntrl:]': Control characters. octal codes 000 through 037, and 177 ('del ').
7. In the same expression, the matched characters cannot be matched for the second time. Because the purpose of regular expressions is to match a character or string, and then match again.
For example, the string "# c #", regular expression "(#.) (. #)" cannot match.
For example, the string "# cc #", regular expression "(. #) (. *) (. #)" can be matched successfully, but the second group can only be empty.
8. "View", that is, lookaround anchor.
To (? = Replaces the left brackets to indicate the sequential view from left to right. For example (? = \ D) indicates that the condition is met when the right side of the current character is a number.
To (? <= Replace left brackets to indicate reverse view from right to left, for example (? <= \ D) indicates that the condition is met when the left side of the current character is a number.
- Forward view :(? =...) And (?!...)
- Reverse view :(? <=...) And (? <!...)
The reverse loop expression must be a string of a fixed length. For example (? <= Word) or (? <= Word | word) Yes, (? <= Word ?) No, because? Matches 0 or 1 length, with an indefinite length.
In PCRE, it can be rewritten (? <= Word | words), but not in perl, because perl strictly requires a fixed length.
9. The most important thing to note about the positioning of "loop View" is that the matching result does not occupy any characters, and it is only an anchoring position.
Example: your name is longshuai MA and your name is longfei MA
Use (? = Longshuai) will be able to pin the empty character before the word "longshuai" in the first sentence, but it will match the blank character before "longshuai,
So (? = Longshuai) long can represent the strings "long ".
So only for the two sentences here, long (? = Shuai) and (? = Longshuai) long is equivalent
10. Greedy match, inert match, and possession priority match
By default, expressions for repeated times are greedy, indicating as many matches as possible.
Some advanced RegEx engines Support inert matching, indicating that there are as few matches as possible, and they can be stopped as long as the conditions are met.
- * +? {M, N}: greedy match (lazy)
- *? +? ?? {M, N }? : Greedy)
- * ++? ++ {M, N} +: possessive)
The possession priority is the same as that of the curing group. As long as the possession is reached, no exchange is allowed and Backtracking is not allowed. For an example, see the following (?> ...) Curing group mode
11. Matching Mode
- (? I): It is case-insensitive and can be used (? -I) cancel this mode. For example "(? I) abc (? -I) cdB "only performs case-insensitive matching on the abc in the middle
- Because (? I) when the brackets are closed, they will become invalid. You can write the case-insensitive parts into the grouping brackets, for example "((? I) abc) cdB ",(? :(? I) abc) cdB = (? I: abc) cdB
- (? X): extend mode, which ignores multiple consecutive spaces and comments to the end of a line.
- (? M): (multiline) The multiline mode changes the matching mode of ^ and $. By default, they match the string header and tail. In this mode:
- ^ Match the string header and line break. To match only the string header, use \.
- $ Matches the string tail, line breaks, and blank characters before line breaks. To match only the end and end of a string, use \ Z. To match only the end of a string, use \ z
- (? S) :( singleline or dotall) single-row mode. Change the matching mode of ".". In the default mode, click ".". The line break cannot be matched. In dotall mode
- (? U): lazy matching mode. The default value is greedy match.
12. Force literal explanation: \ Q... \ E. This sequence forcibly interprets all characters in the middle of the sequence as literal characters, which is highly mandatory.
But perl and pcre are different. In perl, variables can be referenced in the sequence to replace variables, while variable symbols in pcre are also treated as common characters.
13. Common grouping and capturing
- (), $1, $2, $3, $4... in some cases, use \ 1, \ 2, \ 3, \ 4, sed to use & to indicate all matches, and use $ & in perl &
- \ G1, \ g2, \ g3 or \ g {1}, \ g {2}, \ g {3 }.
$1, $2,... is used outside the regular expression, while "\ g1", "\ g2",... is used inside the regular expression.
14. Naming and capturing
- (? :...): Non-naming capture, used only for grouping, cannot be used for reference, also known as non-capturing parentheses. For example, "(1 | one )(? : 2 | two) (3 | three) ", $1 = (1 | one), $2 = (3 | three)
- (? <NAME>...): NAME capture. It is also named after group capture, just like assigning values to variables. You can use the \ k <NAME>, \ k'name', or \ g {NAME} method to reference
- (?> ...) : Curing group. Once a match is successful, the content will never be returned (it is easy to understand with backtracing ).
For example, "hello world" can be matched by "El. * world", but cannot be matched by "El (?>. *) World "match.
Under normal circumstances, ". *" matches all content, and then releases a matched content to the backend until the space "character. After the group is fixed, the matched content will never be returned, so it cannot be traced back.
Go back to the Linux series article outline: workshop!