Regular Expression: A Regular Expression
Role: used for special string operations
Features: it is used to indicate some code operations by symbols of some features. This simplifies writing.
Learning regular expressions is to learn the use of some special symbols.
Specific operation functions:
1. Match: matches () of the string class ()
2. Cut: Split () of the string class ()
3. Replace: replaceall () of the string class ()
4. Get: retrieve the strings that comply with the rules.
(1) encapsulate regular expressions into objects
(2) associate the regular object with the string to be operated.
(3) obtain the regular expression matching engine after Association.
(4) use the engine to operate the strings that comply with the rules, such as extracting them.
{N} specifies the remaining nine digits, which cannot be more than less;
{N, m} can only be n to the next M-bit. the string to be matched can be extra M-bit, but not less.
{N,} indicates all
"(.) \ 1" Completes cutting by overlapping words. In order to reuse the results of rules, rules can be encapsulated into a group.
Complete with (), and the group is numbered. Starting from 1,
To use an existing group, you can use \ n (n is the group number.
"\\\\" Cut \\
"+": "+" Occurs once or multiple times. If you use ".", you must use "\." for escape.
Class regexdemo {public static void main (string [] ARGs) {// demo (); checktel (); // splitdemo ("C: \ ABC \ a.txt ", "\\\\"); // cut by \\// splitdemo ("erktyqquiio ","(.) \ 1 "); // replacealldemo (" we54544456464sy455446dsdsa5451 "," \ D {0,} "," # ");} public static void demo () {string STR = "56566565666666464"; // char [] Buf = Str. tochararray (); string Reg = "[1-9] [[0-9] & [^ A-Za-Z] {4, 14}"; Boolean B = Str. matches (REG); system. out. println (B);} // matches the mobile phone number 13xxx 15xxx 18 xxxpublic static void checktel () {string Tel = "151723174065 "; string telreg = "1 [358] \ D {3,}"; Boolean flag = Tel. matches (telreg); system. out. println (FLAG);} public static void splitdemo (string STR, string REG) {// string STR = "zhangsan Lisi wangwu"; // STR = "C: \ ABC \ a.txt "; // string Reg =" + "; // string [] arr = Str. split (REG); For (string S: ARR) {system. out. println (s) ;}} public static void replacealldemo (string STR, string Reg, string newstr) {STR = Str. replaceall (Reg, newstr); system. out. println (STR );}}