-1

I've seen multiple questions/answers regarding phone number authentication, which is great, but all of them only seen to work IF the string is strictly a phone number. In my case, we might have receive a string like this:

"Okay, I've got your number as 313-123-1234. Now lets..."

The formatting of the phone number can vary as well, so I think it would be better if the regex could handle "any sequence of 10 numbers, ignoring ( ) - ."

I've tried a few different things, including:

"^((\\+\\d{1,2}|1)[\\s.-]?)?\\(?[2-9](?!11)\\d{2}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}$"

and

"/[0-9]{10}\\b/"

Among others, which, again, seen to work if it's the phone number by itself, but when I call:myStrig.matches(myRegex); on the full string, it always returns false.

With that being said, is there either a native solution, such as PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber) that would work with the example phrase mentioned above, or what is a regex that would actually work in this case?

Thanks!

Felipe
  • 5,126
  • 5
  • 18
  • 21
  • Try replacing `^` with `(?<!\\w)` and `$` with `(?!\\w)`. – Wiktor Stribiżew Nov 15 '17 at 15:32
  • It didn't work. I tested against "This is my phone, write it down (847) 456-3450. Make sure you have it." And with your replacement: `"(?<!\\\\w)((\\+\\d{1,2}|1)[\\s.-]?)?\\(?[2-9](?!11)\\d{2}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}(?!\\\\w)"` – Felipe Nov 15 '17 at 15:42
  • [`(?<!\w)(?:\(\d{3}\) ?|\d{3}[ -]?)\d{3}[- ]?\d{4}\b`](https://regex101.com/r/t2GRz7/2)? – ctwheels Nov 15 '17 at 15:50
  • I actually just found that there's an issue with `Pattern.matches()` in Android. I was able to work around that and your first answer, replace `^` and `$` seems to work with all scenarios. Thank you! – Felipe Nov 15 '17 at 16:27
  • @Felipe Why did you use `(?<!\\\\w)` instead of `(?<!\\w)`? I wrote `(?<!\\w)`, just what should be put into the Java string literal. – Wiktor Stribiżew Nov 15 '17 at 17:27
  • @WiktorStribiżew, That was just a mistake while placing it into/from the IDEA. If you see below, I actually said that your suggestion allowed me to fix the issue along something else, and I credited you for it. But I just saw, why did you mark this as duplicated? And if it really is considering both problems, can you please link the other question so myself and others can see the answer? – Felipe Nov 16 '17 at 16:28

2 Answers2

-1

Try using this pattern :

String REGEX_PHONE_NUMBER_ALL = "(?:\\(\\d{3}\\)|\\d{3}[-]*)\\d{3}[-]*\\d{4}";
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • I tried this against ""This is my phone, write it down (847) 456-3450. Make sure you have it.", but it didn't work either. – Felipe Nov 15 '17 at 15:43
  • maybe you could try to replace "(" and ")" to empty before you check against the regular expression :) – diegoveloper Nov 15 '17 at 15:55
-1

THE ANSWER

So, first of all, there's seems to be a known issue with Android's Pattern.matches(string, regex);. Instead, you should use:

    Pattern p = Pattern.compile(myRegex);
    Matcher m = p.matcher(myString);

    if(m.find()) {
        // true
    }

Secondly, my original regex was still not working for all the scenarios I needed, but thanks to Wiktor Stribiżew suggestion, it now works.

He suggested replacing ^ with (?<!\\w) and $ with (?!\\w).

My regex now looks like this:

(?<!\w)((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}(?!\w)

And it works!

For reference, here are some of the basic scenarios I tested it with:

  • (313) 456 3450
  • (313) 456-3450
  • (313)-456-3450
  • 313-456-3450
  • 313.456.3450
  • 313 456 3450
  • 3134563450
Felipe
  • 5,126
  • 5
  • 18
  • 21