0

How to use this with String.matches(...........)

^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$

It would match the following examples and much more:

18005551234     
1 800 555 1234    
+1 800 555-1234

+86 800 555 1234    
1-800-555-1234    
1 (800) 555-1234    
(800)555-1234    
(800) 555-1234    
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678    
8005551234 x5678    
1    800    555-1234    
1----800----555-1234
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
mdaza
  • 21
  • 5
  • here is link of this regex http://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number – mdaza Aug 09 '16 at 07:46
  • 1
    Escape the backslashes. I'd also recommend replacing all `\d` with `[0-9]`. In `matches`, you do not need `^` and `$`. See [*How to use regular expression in android*](http://stackoverflow.com/questions/10327901/how-to-use-regular-expression-in-android). – Wiktor Stribiżew Aug 09 '16 at 07:47

2 Answers2

0

First, you need to escape the first + in your pattern.

Then, make delimiters optional and allow more than one in consequence to match all variants like 1 (800) 555-1234, (800) 555-1234, and 18005551234.

The regex would be:

^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: ?x(\d+))?\s*$

Demo: https://regex101.com/r/pV4hL9/1

Finally, in Java, escape backslashes with backslashes: https://ideone.com/L6bNA9

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
0

You could use: libphonenumber
And if you're developing for android 4.0 (Ice Cream Sandwich) or later it is built in within the android framework.

(This was mentioned before by someone else, but they removed the answer)

Olian04
  • 6,480
  • 2
  • 27
  • 54