• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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
0
KavyaVS
Asked:
KavyaVS
  • 6
  • 6
2 Solutions
 
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now