Regular expression for Business Phone number

I am using this regular expression for business phone number
 "^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$".
But this is accepting '1' in the 4th place (The first digit in exchange code).But it should allow only 2-9 in the 4th place.



Please let me know the mistake in it.

Thanks,
Rama
KavyaVSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkofteCommented:
"^\(?([2-9][0-8][2-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$".
0
käµfm³d 👽Commented:
That's because you made the exchange optional:

([2-9][0-9]{2})?

Open in new window


The question mark after the parentheses makes everything inside the parentheses optional.

What is it you were trying to achieve with the question mark?

P.S.

You should be receiving something like this:
 Screenshot
Notice how the exchange is empty, the one is being picked up as part of the subscriber number, and the extension now has a value.
0
KavyaVSAuthor Commented:
Thanks Kaufmed. It is working now.I deleted that question mark after exchange code and now it is not accepting 1 as the first digit in the exchange code.

You gave me the regular expressions for Home Phone and Business Phone number.
Home Phone:"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})$"
Business Phone:"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})([0-9]{4})([0-9]{0,4})$"

So the Home Phone expression also have mistake in it.

Thanks.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

käµfm³d 👽Commented:
You gave me the regular expressions for Home Phone and Business Phone number.
I remember  :  )

So the Home Phone expression also have mistake in it.
Same issue. You have a question mark after exchange.
0
KavyaVSAuthor Commented:
I deleted the question mark after exchange code in Home Phone regular expression.
It has question mark after area code also. Is it O.k


I am using the following regular expression for street Address
\d{1,3}.?\d{0,3}\s[a-zA-Z]{2,30}(\s[a-zA-Z]{2,15})?([#\.0-9a-zA-Z]*)?  
Can you suggest me any corrections to accept all kind of street addresses.

Thanks
0
käµfm³d 👽Commented:
Can you suggest me any corrections to accept all kind of street addresses.

I'm afraid I cannot because I am not familiar with all the different accepted possibilities. I know common ones such as those for houses or apartments, but I don't know all the characters that would be involved. You may consider looking into some address normalization product.
0
KavyaVSAuthor Commented:
I know common ones such as those for houses or apartments.

Please give it me whatever you know.You mentioned that you know common ones such as those for
houses or apartments.I want to try that one.

Thanks.

0
käµfm³d 👽Commented:
You mentioned that you know common ones such as those for
houses or apartments.I want to try that one.

To clarify, I meant I understand what a common house address (US) consists of. You can try below, but I can't guarantee it will catch every address.
\d+\s+\w+\s+[a-zA-Z]{2,}\.?(?:\s+[a-zA-Z]{2,3})?(?:\s+[a-zA-Z]+\.?\s+\d*)?

// Breakdown
\d+           Bldg Number
\s+           At least a space
\w+           Street Name
\s+           At least a space
[a-zA-Z]{2,}  Street Designator (e.g. AVE, ST, RD, etc.)
\.?           Optional dot (some people abbreviate with dot)

// Optional ( ? )
\s+           At least a space
[a-zA-Z]{2,3} Direction Indicator (e.g. NW, SW, etc.); This might need tweaking

// Optional ( ? )
\s+           At least a space
[a-zA-Z]+     Secondary Designator (e.g. STE, APT, etc)
\.?           Optional dot (some people abbreviate with dot)
\s+           At least a space
\d*           Secondary Designator Number (e.g. 12, as in APT 12; not all secondary designators have numbers, so digits is optional [ * ])

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
Actually, I forgot to make "street name" allow multiple words. Use this instead:
\d+\s+[\w\s-]+\s+[a-zA-Z]{2,}\.?(?:\s+[a-zA-Z]{2,3})?(?:\s+[a-zA-Z]+\.?\s+\d*)?

// Breakdown
\d+           Bldg Number
\s+           At least a space
[\w\s-]+      Street Name
\s+           At least a space
[a-zA-Z]{2,}  Street Designator (e.g. AVE, ST, RD, etc.)
\.?           Optional dot (some people abbreviate with dot)

// Optional ( ? )
\s+           At least a space
[a-zA-Z]{2,3} Direction Indicator (e.g. NW, SW, etc.); This might need tweaking

// Optional ( ? )
\s+           At least a space
[a-zA-Z]+     Secondary Designator (e.g. STE, APT, etc)
\.?           Optional dot (some people abbreviate with dot)
\s+           At least a space
\d*           Secondary Designator Number (e.g. 12, as in APT 12; not all secondary designators have numbers, so digits is optional [ * ])

Open in new window

0
KavyaVSAuthor Commented:
Thanks for your detailed explanation and time.
783,Frederick Stamm Ct.,Apt#2 is not working.

0
käµfm³d 👽Commented:
Ok. So we need to add commas and hashes ( # ). Let's see if this functions better:
\d+(?:\s*,)?[\w\s-]+\s+[a-zA-Z]{2,}\.?(?:\s+[a-zA-Z]{2,3})?,?(?:\s*[a-zA-Z]+\.?\s*#?\s*\d*)?

Open in new window

0
KavyaVSAuthor Commented:
Thanks.It's working now.
0
KavyaVSAuthor Commented:
Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.