first, the introduction
What is a regular expression: a rule that can be represented using a single string. The most common regular expression verifies that the e-mail string is: ^[a-za-z0-9_-]+@[a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +$, and e-mail is also a string that implements a string matching checksum for another string. In actual development, regular expressions are often used to check whether certain strings are compliant, such as user name, password, mobile number ...
the grammatical rules of regular expressions
1. API documentation: interface implemented by Java.util.regex.Pattern: Serializable
2. Regular expressions distinguish between uppercase and lowercase letters.
3. Character and its meaning
|
Character |
Meaning |
Example |
|
X |
Character X |
"A", the string content that needs to match is "a" |
|
\ |
Escape character |
"\ t" tab, "\ n" line breaks,
"\ r" carriage return, "\" backslash \ |
|
[ABC] |
Character A|b|c one of the |
A |
|
[^ABC] |
Take the counter on top |
"T" |
|
[A-za-z] |
An uppercase or lowercase letter |
A |
|
[0-9] |
A number |
"0" |
|
[A-za-z_0-9] |
A letter or number or "_" |
"9" |
|
\d |
predefined character classes [0-9] |
"0" |
|
\w |
Predefined character classes
[A-za-z_0-9] |
"Y" |
|
^ |
The beginning of a line |
^[abc][0-9]$ need to match the content from [ABC] this position to start |
|
$ |
End of Line |
The content that needs to match ends with [0-9] |
|
\b |
Boundary matching-word boundaries |
"\b[abc]\b" a|b|c the right and left sides of the need for non-word characters [a-za-z_0-9] |
|
X? |
X appears one time or does not appear |
"A?" A character A, or a A is not |
|
x* |
X appears multiple times or does not appear |
"A *" multiple characters A, or a A is not |
|
x+ |
X appears one or more times |
"A +" a character A or more characters a |
|
X{n} |
X appears exactly n times |
"A{3}" character a appears 3 times |
|
X{n,} |
X appears at least n times |
"A{3,}" character a appears at least 3 times |
|
X{N,M} |
X appears at least n times, but not more than m times |
"a{3,6}" character a appears at least 3 times, but not more than 6 times |
Three, regular expression rule matching practice
1. [0-9]{6,12} with a length of 6 digits to 12 digits
2.1[34578][0-9]{9} The 1th digit is 1, 2nd is 3 or 4 or 5 or 7 or 8, followed by 9 digits
3. A*b multiple A followed by the end of B, or a does not appear with the end of B "Aaab" true "abc" false
common methods for designing regular expressions in string classes
1. Boolean matches (string regex) tells whether this string matches a given regular expression
2. string Split (Stringregex) splits this string according to the match of a given regular expression, split and placed in the string[] array
3. String ReplaceAll (string regex,stringreplacement) replaces all substrings of this string with the given regular expression with the given replacement
Demo:
Results:
number-related regular expressions
1. Matching positive integer: "\\d+"
2. Match positive decimal: "\\d+\\.\\d+"
3. Matching negative integers: "-\\d+"
4. Match minus decimal: "-\\d+\\.\\d+"
5. Matches a positive number that retains a two-digit decimal number: "\\d+\\.\\d{2}"
9. Matches a positive number that retains a 1-3-digit decimal number: "\\d+\\.\\d{1,3}"