简体   繁体   中英

Checking a number with regular expressions in Java

I have a number "8756342536" . I want to check whether the number starts with "8" , contains 10 digits all of it is numeric, using regular expression.

What pattern would I need for this scenario in Java? Thanks in advance.

使用Stringmatchs(...)方法:

boolean b = "8756342536".matches("8\\d{9}");

Use this expression:

^8\d{9}$

meaning an 8 followed by exactly 9 digits. So for example:

String number = ...
if (number.matches("^8\\d{9}$")) {
  // it's a number
}

Given the number is in String form:

String number = "8756342536";
boolean match = number.matches ("^[8][0-9]{9}$");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM