Home > Article > Web Front-end > Regular expressions (grammar)
Now I will bring you a regular expression (grammar recommendation). Let me share it with you now and give it as a reference for everyone.
Construction summary of regular expression
Construction matches
Character
x Character x
\\ Backslash character
\0n Character n with octal value 0 (0 <= n <= 7)
\0nn Character nn with octal value 0 (0 <= n <= 7)
\0mnn character mnn with octal value 0 (0 <= m <= 3, 0 <= n <= 7)
\xhh with hexadecimal value Character hh with value 0x
\uhhhh Character hhhh with hexadecimal value 0x
\t Tab character ('\u0009')
\n Newline (newline) character ('\u000A ')
\r Carriage return character ('\u000D')
\f Form feed character ('\u000C')
\a Alarm (bell) character ('\u0007')
\ e escape character ('\u001B')
\cx Control character corresponding to x
Character class
[abc] a, b or c ( Simple class)
[^abc] Any character except a, b or c (negative)
[a-zA-Z] a to z or A to Z, inclusive (range)
[a-d[m-p]] a to d or m to p: [a-dm-p] (union)
[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)
Predefined character classes
. Any character (which may or may not match line terminators)
\ d Numbers: [0-9]
\D Non-numbers: [^0-9]
\s White space characters: [ \t\n\x0B\f\r]
\S Non-white space characters :[^\s]
\w Word characters: [a-zA-Z_0-9]
\W Non-word characters: [^\w]
POSIX character class (only US- ASCII)
\p{Lower} Lowercase alphabetic characters: [a-z]
\p{Upper} Uppercase alphabetic characters: [A-Z]
\p{ASCII} All ASCII: [\x00-\x7F]
\p{Alpha} Alphabetic characters: [\p{Lower}\p{Upper}]
\p{Digit} Decimal digits: [0-9]
\p{Alnum} Alphanumeric characters :[\p{Alpha}\p{Digit}]
\p{Punct} Punctuation: !"#$%&'()* ,-./:;<=>?@[\] ^_`{|}~
\p{Graph} Visible characters: [\p{Alnum}\p{Punct}]
\p{Print} Printable characters: [\p{Graph}\x20 ]
\p{Blank} Space or tab character: [ \t]
\p{Cntrl} Control character: [\x00-\x1F\x7F]
\p{XDigit} Hexadecimal System digits: [0-9a-fA-F]
\p{Space} Blank characters: [ \t\n\x0B\f\r]
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} Equivalent to java.lang.Character.isWhitespace()
\p{javaMirrored} Equivalent to java.lang.Character.isMirrored()
Classes for Unicode blocks and categories
\p{InGreek} Characters in Greek block (simple block)
\p{Lu} Uppercase letters (simple category)
\p{Sc} Currency symbols
\P{InGreek} All characters , except in Greek blocks (negation)
[\p{L}&&[^\p{Lu}]] All letters, except uppercase letters (minus)
Boundary matcher
^ Beginning of line
$ End of line
\b Word boundary
\B Non-word boundary
\A Beginning of input
\ G The end of the previous match
\Z The end of the input, only used for the last terminator (if any)
\z The end of the input
Greedy quantifier
X? Times
X{n,} X, at least n times
X{n,m} #X?? #X{n,}? X, at least n times
X{n,m}?
X? #X{n,} X, at least n times
X{n,m} X, at least n times, but not more than m times
Logical operator
XY X followed by Y
X|Y X or Y
(X) #\n Any matching nth capture group
Quotes
\Q Nothing, but quotes all characters up to \E \E Nothing, but ends with \Q Starting reference
Special construction (non-capturing)
(?:X) X, as a non-capturing group
(?idmsux-idmsux) Nothing, but will match the flag idms u The non-capturing group of the given flag i d m s u x on - off
(?=X) X) X, through a zero-width positive lookbehind
(?
Backslash, escape and quote
The backslash character ('\') is used for quote escape constructs, as shown in the table above defined, but also used to refer to other characters that will be interpreted as unescaped constructs. Therefore, the expression \\ matches a single backslash, and \{ matches an opening parenthesis. It is an error to use backslashes before any alphabetic characters that do not represent an escape construct; they are reserved for future extensions to the regular expression language. A backslash can be used before a non-alphabetic character, regardless of whether the character is part of an unescape construct. As required by the Java Language Specification, backslashes in strings in Java source code are interpreted as Unicode escapes or other character escapes. Therefore, you must use two backslashes in the string literal to indicate that the regular expression is protected from being interpreted by the Java bytecode compiler. For example, when interpreted as a regular expression, the string literal "\b" matches a single backspace character, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and will cause a compile-time error; to match the string (hello), the string literal "\\(hello\\)" must be used.
Character ClassesCharacter classes can appear within other character classes and can contain the union operator (implicit) and the intersection operator (&&). The union operator represents a class that contains all characters in at least one of its operand classes. The intersection operator represents a class that contains all characters that are in both of its operand classes.
The precedence of character class operators is as follows, arranged from highest to lowest:
1 Literal value escaping \x
2 Grouping [...]3 Range a-z 4 Union [a-e][i-u] 5 Intersection [a-z&&[aeiou]]
Note that different sets of metacharacters are actually is inside the character class, not outside the character class. For example, the regular expression . loses its special meaning within a character class, while the expression - becomes a range forming metacharacters.
Line terminator
Line terminator is a sequence of one or two characters that marks the end of a line for a sequence of input characters. The following codes are recognized as line terminators: New line (line feed) character ('\n'), Carriage return character ("\r\n") followed by a new line character, A single carriage return ('\r'),
Next line character ('\u0085'),Line separator ('\u2028') or
Paragraph separator ('\ u2029). If UNIX_LINES mode is activated, the newline character is the only recognized line terminator.
If the DOTALL flag is not specified, the regular expression . can match any character (except line terminators).
By default, the regular expressions ^ and $ ignore line terminators and only match the beginning and end of the entire input sequence, respectively. If MULTILINE mode is activated, ^ matching occurs at the beginning of the input and after the line terminator (at the end of the input). When in MULTILINE mode, $ matches only before a line terminator or at the end of the input sequence.
Groups and Captures
Capturing groups can be numbered by counting their opening brackets from left to right. For example, in the expression ((A)(B(C))), there are four such groups: 1 ((A)(B(C))) 2 \A 3 (B(C))
4 (C) Group zero always represents the entire expression.
The capturing groups are named this way because in the match, every subsequence of the input sequence that matches these groups is saved. The captured subsequence can later be used in an expression via a Back reference, or can be obtained from the matcher after the matching operation has completed.
The capture input associated with a group is always the subsequence that most recently matched the group. If a group is calculated again due to quantization, its previously captured value (if any) will be retained when the second calculation fails. For example, comparing the string "aba" with the expression (a(b)?) match, sets the second group to "b". At the beginning of each match, all captured input is discarded.
A group starting with (?) is a pure non-capturing group, which does not capture text and does not count against the group total.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
related articles:
What are the common scenarios in which closures can be exploited in JS? (Picture and text tutorial)
How to copy an object in js? (Picture and text tutorial)
Example of using js to implement a message board (code provided)
The above is the detailed content of Regular expressions (grammar). For more information, please follow other related articles on the PHP Chinese website!