tags:

views:

72

answers:

2

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.

+5  A: 

Use String's matches(...) method:

boolean b = "8756342536".matches("8\\d{9}");
Bart Kiers
+4  A: 

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
}
cletus