0

I have created a simple regular expression that matches a phone number.I have a group inside where I choose between space,dot and hyphen if NY input string contains more than one group...it does not match the entire result.

           ([\(]?\d+[\)]?(-|\s|\.)?\d+)+

The input is (123)1234-1236 12345 or something of that sort...the actual input is from Tesseract OCR and is unreliable at best.

Can you help me create a better easy to use and understandable regex or improve the regex to match phone numbers.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
  • 3
    possible duplicate http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation You're likely not going to get an 'easy to use and understandable regex' for this, especially if you start considering international numbers. I'd suggest following the accepted answer on that question. – Tyler Lee Nov 06 '13 at 13:45
  • If I understand your question, you can simplify the regex by replacing all the hyphens, spaces, etc, to one single character, and then matching on that. Much more maintainable and understandable that way. – EdgeCase Nov 06 '13 at 13:48
  • The string has a lot of details...cannot replace a hyphen elsewhere...could cause something else to be read wrongly – vamsiampolu Nov 06 '13 at 14:19

1 Answers1

0

Here is a regular expression that exactly matches the date format you specified above.

^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$

Matches: (910)456-7890 | (910)456-8970 x12 | (910)456-8970 1211 Non-Matches: (910) 156-7890 | (910) 056-7890 | (910) 556-7890 x

This is the link I referred to. You can find regular expressions in many formats you can pick and choose and modify according to your need.

Poornima
  • 918
  • 5
  • 11
  • Could you have something a bit more detailed than a link for 'this'? What is this? Not everyone can get to all sites, sites may change their pages and the link rots, *this* answer won't show up well when looking for answers. –  Nov 06 '13 at 22:38
  • This link points to a regular expression for the date format (123)1234-1236 12345. And there are some examples with more options for date formats to pick and choose. I dint realize the fact that not everyone can open all the sites. I will update my answer. – Poornima Nov 07 '13 at 13:15
  • At one place I worked, they locked down the internet for a significant portion of the development staff to only StackOverflow (and the maven repo). As such, answers that were only a link to an external resource where almost completely useless. –  Nov 07 '13 at 15:06
  • Right. I have had that experience too. Thanks for pointing it out. – Poornima Nov 07 '13 at 15:11