The meaning of an expression:
1. Characters
x character X. For example a denotes the character a
\ \ backslash character. Write as \\\\ as you write. (Note: Because Java in the first parse, the \\\\ parse into a regular expression \ \, in the second resolution to the \, so generally not 1.1 enumerated to the escape character, including 1.1 \ \, and with the \ 's to write two times)
\0n characters with octal value 0 N (0 <= n <= 7)
\0nn character nn with octal value 0 (0 <= n <= 7)
\0mnn characters with octal value 0 mnn (0 <= m <= 3, 0 <= n <= 7)
\xhh character hh with hexadecimal value of 0x
\uhhhh characters with a hexadecimal value of 0x HHHH
\ t tab (' \u0009 ')
\ n New Line (newline) character (' \u000a ')
\ r return character (' \u000d ')
\f page Break (' \u000c ')
\a Alarm (Bell) symbol (' \u0007 ')
\e Escape character (' \u001b ')
\cx the control that corresponds to X
2. Character classes
[ABC] A, B, or C (simple Class). For example [EGD] means that it contains characters e, G, or D.
[^ABC] Any character except A, B, or C (negation). For example [^EGD] means that no characters e, G, or D are included.
[A-za-z] A to Z or A to Z, the letters at both ends are included (range)
[A-d[m-p]] A to D or M to P:[a-dm-p] (set)
[A-z&&[def]] D, E or F (intersection)
[A-Z&&[^BC]] A to Z, except B and C:[ad-z] (minus)
[A-z&&[^m-p]] A to Z, not M to P:[a-lq-z] (minus)
3. Predefined character Classes(Note that the backslash should be written two times, e.g. \dWrite as \\d) any character
(May or may not match with line terminator)
\d number: [0-9]
\d non-numeric: [^0-9]
\s whitespace characters: [\t\n\x0b\f\r]
\s non-whitespace characters: [^\s]
\w Word character: [a-za-z_0-9]
\w non-word characters: [^\w]
4.POSIX character class(Us-ascii only) (note that the backslash is to be written two times, for example, \p{lower} is written as \\p{lower})
\p{lower} lowercase alphabetic characters: [A-z].
\p{upper} uppercase characters: [A-z]
\P{ASCII} all ascii:[\x00-\x7f]
\p{alpha} alphabetic characters: [\p{lower}\p{upper}]
\p{digit} decimal number: [0-9]
\p{alnum} alphanumeric characters: [\p{alpha}\p{digit}]
\P{PUNCT} punctuation:! " #$%& ' () *+,-./:;<=>[email protected][\]^_ ' {|} ~
\p{graph} visible characters: [\p{alnum}\p{punct}]
\p{print} printable characters: [\p{graph}\x20]
\p{blank} spaces or tabs: [\ t]
\p{cntrl} control characters: [\x00-\x1f\x7f]
\p{xdigit} hex Number: [0-9a-fa-f]
\p{space} white space character: [\t\n\x0b\f\r]
5.java.lang.character class(Simple Java character type)
\p{javalowercase} is equivalent to Java.lang.Character.isLowerCase ()
\p{javauppercase} is equivalent to Java.lang.Character.isUpperCase ()
\p{javawhitespace} is equivalent to Java.lang.Character.isWhitespace ()
\p{javamirrored} is equivalent to java.lang.Character.isMirrored ()
6.Unicode blocks and categories of classes
\p{ingreek} characters in Greek block (simple block)
\P{LU} capital letters (simple category)
\P{SC} currency symbol
\p{ingreek} all characters except (negation) in the Greek block
[\p{l}&&[^\p{lu}]] All letters, except capital letters (minus)
7. Boundary Matching device
^ The beginning of the line, please use ^ at the beginning of the regular expression. For example: ^ (ABC) represents a string that begins with ABC. Note that when compiling the parameter MULTILINE, such as the Pattern p = pattern.compile (regex,pattern.multiline);
End of the $ line, use at the end of the regular expression. For example: (^BCA). * (abc$) denotes a line ending with a BCA with ABC.
\b Word boundaries. For example, \b (ABC) indicates that the beginning or end of a word contains ABC, (ABCJJ, JJABC can match)
\b A non-word boundary. For example, \b (ABC) indicates that the middle of a word contains ABC, (JJABCJJ matches and JJABC, ABCJJ does not match)
\a the beginning of the input
\g the end of the previous match (personal feeling this parameter is useless). For example, \\Gdog indicates that finding the dog at the end of the previous match, if not, looks from the beginning, noting that it does not match if it is not a dog at the beginning.
\z the end of the input, only for the last terminator (if any)
The line terminator is a sequence of one or two characters that marks the end of the line of the input character sequence.
The following code is recognized as a line terminator:
-New Line (newline) character (' \ n '),
-the carriage return immediately following the new line character ("\ r \ n"),
-Separate carriage return (' \ R '),
-The next line of characters (' \u0085 '),
-Line delimiter (' \u2028 ') or
-Paragraph delimiter (' \u2029 ').
\z End of input
When compiling a pattern, you can set one or more flags, such as
Pattern pattern = pattern.compile (patternstring,pattern.case_insensitive + pattern.unicode_case);
The following Six Flags are supported:
‐case_insensitive: The match character is not case-insensitive, and the flag only considers us ASCII characters by default.
‐unicode_case: When combined with case_insensitive, use UNICODE letters to match
‐multiline:^ and $ match the start and end of a line instead of the entire input
‐unix_lines: When matching ^ and $ in multiline mode, only ' \ n ' is considered a line terminator
‐dotall: When using this flag, the. Symbol matches all characters including the line terminator
‐CANON_EQ: Consider the canonical equivalence of Unicode characters
8.Greedy Quantity Words
X? X, not once or once
X* X, 0 or more times
x+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n times, but no more than m times
9.Reluctant Quantity Words
X?? X, not once or once
X*? X, 0 or more times
X+? X, one or more times
X{n}? X, exactly n times
X{n,}? X, at least n times
X{n,m}? X, at least n times, but not more than m times
10.Possessive Quantity Words
x?+ X, not once or once
x*+ X, 0 or more times
X + + ×, one or more times
x{n}+ X, exactly n times
x{n,}+ X, at least n times
x{n,m}+ X, at least n times, but no more than m times
The difference between greedy,reluctant,possessive is that: (Attention is limited to the process. When blurring)
The greedy quantifier is considered "greedy" because it first reads into the whole fuzzy matching string. If the first match attempt (the entire input string) fails, the match will step back one character at the last digit in the matched string and try again, repeating the process until a match is found or no more remaining characters can go back. Depending on the quantifier used in the expression, the last attempt to match the content is 1 or 0 characters.
However, reluctant quantifiers take the opposite approach: they start at the beginning of the matched string, and then progressively read one character search match at a time. The last thing they tried to match was the entire input string.
Finally, the possessive quantifier always reads the entire input string and attempts to match it once (and only once). Unlike greedy quantifiers, possessive never retreat.
11.Logical operator
XY X followed by Y
X| Y X or Y
(x) x, as a capturing group. For example (ABC) means to capture ABC as a whole
12.Back References
\ n Any matching nth capturing group
Capturing groups can be numbered by calculating their opening brackets from left to right. For example, in an expression ((A) (B (C))), there are four such groups:
1 ((A) (B (C)))
2 \a
3 (B (C))
4 (C)
In an expression, you can refer to the corresponding group by \ n, for example (AB) 34\1 represents AB34AB, (AB) (CD) \1\2 represents AB34CDABCD.
13. References
\ Nothing, but references the following characters
\q nothing, but refers to all characters until \e. The string between QE is used in its entirety (except for 1.1 brokered characters). For example, ab\\q{|} \\\\e
Can match ab{|} \\
\e Nothing, but ends a reference starting from \q
14. Special Construction (non-capture)
(?: x) x, as a non-capturing group
(? idmsux-idmsux) Nothing, but turns the matching flag from on to off. For example: expression (? i) ABC (?-i) def at this point, (? i) open case-insensitive switch, ABC match
Idmsux is described below:
The ‐i case_insensitive:us-ascii character set is case insensitive. (? i)
‐d unix_lines: Open UNIX line break
‐m MULTILINE: Multiline mode (? m)
Unix down-change behavior \ n
Windows down change behavior \ r \ n (? s)
‐u Unicode_case:unicode are not case sensitive. (? u)
‐x COMMENTS: You can use annotations inside the pattern, ignoring the whitespace in the pattern, and "#" until the end (#后面为注解). (? x) For example (? x) ABC#ASFSDADSA can match string ABC
(? idmsux-idmsux:x) X, as a non-capturing group with the given flag on-off. Similar to the above, the above expression can be rewritten as: (? i:abc) def, or (? i) ABC (?-I:DEF)
(? =x) X, through the 0 width of the positive lookahead. 0 width forward assertion, the match is continued only if the sub-expression X matches to the right of the position. For example, \w+ (? =\d) indicates that letters are followed by numbers, but no numbers are captured (no backtracking)
(?! x) x, through a 0-width negative lookahead. 0 width Negative antecedent assertion. The match continues only if the sub-expression X does not match to the right of the position. For example, \w+ (?! \d) indicates that letters are not followed by numbers, and numbers are not captured.
(? <=x) X, through the 0 width of the positive lookbehind. 0 width Positive post assertion. The match continues only if the subexpression X matches at the left of this position. For example, (? <=19) 99 means that 99 is preceded by the number 19, but does not capture the preceding 19. (No backtracking)
(? (? >x) X, as a separate non-capturing group (no backtracking)
The difference between (? =x) and (? >x) is that (? >x) is not backtracking. For example, the string that is matched is abcm
When the expression is a (?: B|BC) m can be matched, and when the expression is a (? >B|BC) is not matched, because when the latter match to B, because it has been matched, the non-capturing group is jumped out, and the characters within the group are not matched again. can speed up.
Explanation of Java Regular expressions