A practical example
1 Public Static voidMain (string[] args) {2 //Simple Practice3System.out.println (" -123". Matches ("-?\\d+")));4System.out.println ("+5678". Matches ("(-|\\+)? \\d+"));5 //QQ Number6System.out.println ("174678839431". Matches ("\\d{5,12}"));7 8 //Fixed phone number9System.out.println ("010 88886666". Matches ("\ \ 0\\d{2}[)-]?\\d{8}"));TenSystem.out.println ("010-88886666". Matches ("\ \ 0\\d{2}[)-]?\\d{8}")); OneSystem.out.println ("(010) 88886666". Matches ("\ (? 0\\d{2}[)-]?\\d{8}")); A - //number or letter (length limit 8-12) -System.out.println ("zhengbin123". Matches ("^[a-za-z0-9]{8,12}$"))); the - //rule-compliant IP address -System.out.println ("8.8.8.256". Matches ("(2[0-4]\\d|25[0-5]|[ 01]?\\d\\d?).) {3} (2[0-4]\\d|25[0-5]| [01]?\\d\\d?])); - + //only Chinese characters can be entered -System.out.println ("Man-Mo in Zhengzhou". Matches ("^[^\\w^[email protected]#$%^&* () {}\\\\|~ ',./';:; ',. , ~! ("") "<> ']{2,4}$")); +}
Ii. Expression Usage Rules table
Character
B |
Specify character B |
\xhh |
Characters with a hexadecimal value of oxhh |
\uhhhh |
Hexadecimal representation of Unicode characters as Oxhhhh |
\ t |
TAB tab |
\ n |
Line break |
\ r |
Enter |
\f |
Page change |
\e |
Escaping (Escape) |
Character class
. |
Any character |
[ABC] |
Any character that contains a, B, and C (same as A|b|c) |
[^ABC] |
Any character except A, B, and C (negation) |
[A-za-z] |
Any character (range) from A to Z or from A to Z |
[Abc[hij]] |
Any A, B, C, H, I, and J characters (same as A|B|C|H|I|J) (consolidated) |
[A-z&&[hij]] |
Any h, I or J (cross) |
\s |
Whitespace (Space, tab, line feed, page feed, and carriage return) |
\s |
Non-whitespace character ([^\s]) |
\d |
Number [0-9] |
\d |
Non-digital [^0-9] |
\w |
Word character [a-za-z0-9] |
\w |
Non-word character [^\w] |
logical operators
Xy |
Y followed by X. |
X| Y |
X or Y |
X |
Capture Group. I and capturing groups can be referenced in expressions using \i |
Boundary Match character
^ |
Start of a row |
$ |
End of Line |
\b |
The boundary of the word |
\b |
The boundaries of non-words |
\g |
The end of the previous match |
Third, attention
Insert a normal backslash in Java, which should be ' \\\\ '
The ' \* ' in the table above is ' \\* ' in Java
Regex Regular Expression Learning notes