Example of using a Java regular expression to determine a mobile phone number

Source: Internet
Author: User

Therefore, we need to clarify how many number segments have been opened. The country number segments are allocated as follows:

Mobile: 134, 135, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188

China Unicom: 130, 131, 132, 152, 155, 156, 185, 186

China Telecom: 133, 153, 180, 189 (1349 Weitong)

Now we can test the regular expression matching,

Import java. io. IOException;

Import java. util. regex. Matcher;

Import java. util. regex. Pattern;

Public class ClassPathResource {

Public static boolean isMobileNO (String mobiles ){

Pattern p = Pattern. compile ("^ (13 [0-9]) | (15 [^ 4, \ D]) | (18 [-9]) \ d {8} $ ");

Matcher m = p. matcher (mobiles );

System. out. println (m. matches () + "---");

Return m. matches ();

}

Public static void main (String [] args) throws IOException {

System. out. println (ClassPathResource. isMobileNO ("12016155153 "));

}

}

Method 2:

Import java. util. regex. Matcher;

Import java. util. regex. Pattern;

String value = "mobile phone number ";

String regExp = "^ [1] ([3] [0-9] {1} | 59 | 58 | 88 | 89) [0-9] {8} $ ";

Pattern p = Pattern. compile (regExp );

Matcher m = p. matcher (value );

Return m. find (); // boolean

Method 3:

String check = "^ ([a-z0-9A-Z] + [-|.]?) + [A-z0-9A-Z] @

([A-z0-9A-Z] + (-[a-z0-9A-Z] + )?.) + [A-zA-Z] {2,} $ ";

Pattern regex = Pattern. compile (check );

Matcher matcher = regex. matcher (email );

Flag = matcher. matches ();

} Catch (Exception e ){

Flag = false;

}


Return flag;

}

/**

* Verify the mobile phone number

* @ Param mobiles

* @ Return [0-9] {5, 9}

*/

Public static boolean isMobileNO (String mobiles ){

Boolean flag = false;

Try {

Pattern p = Pattern. compile ("^ (13 [0-9]) | (15 [^ 4, D]) | (18 [-9]) d {8} $ ");

Matcher m = p. matcher (mobiles );

Flag = m. matches ();

} Catch (Exception e ){

Flag = false;

}

Return flag;

}


Public static boolean isNum (String number ){

Boolean flag = false;

Try {

Pattern p = Pattern. compile ("^ [0-9] {5} $ ");

Matcher m = p. matcher (number );

Flag = m. matches ();

} Catch (Exception e ){

Flag = false;

}

Return flag;

}

}

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.