Mobile phone number Regular expression

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.