The code is as follows:
1PackageTestregex;23ImportJava.util.regex.Matcher;4ImportJava.util.regex.Pattern;56PublicClassTest01 {7/**8* Mobile phone number Regular expression: "^[1][3,5,7,8][0-9]\\d{8}$"9* ^: Regular start10* $: Regular end11* [1]: The first number of mobile phone numbers must be 112* [3,5,7,8]: Mobile Number The second number must be: the number in parentheses13* [0-9]\\d{8}: The last number can be any number of 0-9: {8}: represents exactly 914* \: Conversion character15* \d: Indicates the previous number is the number 0-916* X{n}: Exactly n number note: zero-based17*/18PublicStaticvoidMain (string[] args) {19//Mobile phone number Regular expression20String str= "^[1][3,5,7,8][0-9]\\d{8}$";22//The correct mobile phone numberA String s = "15188888888";24//The wrong phone number, because the start is 2,String s2 = "25123456789";2627/**28* pattern is the mode type,29* Compile (Regular) method pre-compile regular,30* Get a Matcher object31*/Pattern p =Pattern.compile (str);33//The Matcher () method in pattern matches the string to match with the regular match IMatcher m=P.matcher (s);Matcher m2=p.matcher (S2); The Matches () method in the Matcher class determines if the match succeeds to the bo= m.matches (); $ Boolean bo2= m2.matches (); // Output match result: true for success, false for failed System.out.println (bo); System.out.println (BO2); *}
Operation Result:
Mobile phone number Regular expression