2

I have this string:

+1(333) 456-7890

I want to match it with a regular expression. This is what I have now for my regex pattern:

Pattern p1 = Pattern.compile("((\\+{0,}[0-9]{0,3}( |-)?)?)(\\(?[0-9]    
 {3}\\)?|[0-9]{3}( |-)?)([0-9]{3}( |-)?)([0-9]{4}|[a-zA-Z0-9]{7})");

It is supposed to recognize any phone number pattern with potential dashes or spaces in the middle, that could be at least 10 digits (or letters), with no country code and at most 13 digits with a country code.

My pattern seems to match certain cases and not others such as the one previously stated. I'm really stumped, any help would be appreciated.

dda
  • 6,030
  • 2
  • 25
  • 34
  • Is this in Java? You should Google, "online visual java regex tester" or something similar. Lots of online tools available to help with this. – lurker Nov 11 '16 at 17:03
  • not really what i was asking but thanks – SJackson193 Nov 11 '16 at 17:08
  • i wanted to see if anyone could take a look at it and give some insight onto why its not working – SJackson193 Nov 11 '16 at 17:09
  • I know, you were indicating you have a problem with your regex, although you didn't ask anything. ;) I was just offering a handy tool that would enable you to solve it on your own. It was just a suggestion. I did try one of the online tools on your regex and it claimed it had an issue with it and cited the position the issue occurred. But since you weren't clear on what language you're using, that's about as far as it goes. Regex conventions can be somewhat different in different languages. – lurker Nov 11 '16 at 17:10
  • it is in java any ideas on how to fix it? – SJackson193 Nov 11 '16 at 17:19
  • @SJackson193 can you put different sample patterns you want to match? – Federico Piazza Nov 11 '16 at 17:53
  • I've used this one before. http://stackoverflow.com/a/123666/5130921 viewed it on https://www.debuggex.com/ to see how it worked – Aarjav Nov 11 '16 at 18:58

1 Answers1

2

Your regex seems overly complex, which is why it probably breaks somewhere. I tried to follow it but in the end it was easier to read your definition and build a new one from scratch:

(\+\d{1,3}[- ]*)?(\(?\d\d\d\)?)[- ]*(\d\d\d[- ]?\d\d\d\d|[a-zA-Z0-9]{7})

This matches the following test cases:

+1(333) 456-7890

+1-(212) 555-0198

+1 212 555-0198

+1 212-ILOVEUU

Depending on your own test cases, this might be enough. Or not.

(\+\d{1,3}[- ]*)?     // Optional +xxx international prefix plus dash/space
(\(?\d\d\d\)?)[- ]*   // three-digit area code with optional parens
(\d\d\d[- ]?\d\d\d\d| // Either 7 digits, with optional dash
[a-zA-Z0-9]{7})       // or 7 letters/digits
Community
  • 1
  • 1
dda
  • 6,030
  • 2
  • 25
  • 34
  • @SJackson193 `Your regex seems overly complex...I tried to follow...` Which is a big signal that regex is probably not the right tool here. – Kenneth K. Nov 11 '16 at 17:59
  • 1
    Not always. Beginners with regex are usually very good at producing unwieldy monsters... :-) It's not always the fault of the tool. The OP shows in his regex signs of not really understanding what he's doing. – dda Nov 11 '16 at 18:01