473,320 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Regular Expression for a Phone Number


The problem is in the word boundary \b. A leading "("
will match as a word boundary before it gets to the test
for a "(". Changing the expression to:

(?n)(\b|\()1?[\s-]?\(?(?<areaCode>[2-9]\d\d)?\)?[\s-]?(?
<firstThree>[2-9]\d\d)(?:\s|-)?(?<lastFour>\d{4})\b

will allow the leading "(" to be captured in the match,
but has the uninteded consequence of matching "(1 (219)
555-5555)" as well, so you probably need to use something
ilke this:

(?n)((\()|(\b1?[\s-]?\(?))(?<areaCode>[2-9]\d\d)?\)?[\s-]?
(?<firstThree>[2-9]\d\d)(?:\s|-)?(?<lastFour>\d{4})\b

I didn't do a lot of testing with this expression, though,
so make sure you run through all your previous tests
before using it.

It looks like the revised expression runs about 25% slower
than the original, so if you need to run this a lot, you
may prefer your original version.
Brian Davis
www.KnowDotNet.com - tutorials, tips, tricks, and tools
for .NET

-----Original Message-----
Hello,

I'm trying to write a regular expression that will catch
and group a US phone number in pretty much any format. (eg
1-xxx-xxx-xxxx or (xxx) xxx-xxxx or xxxxxxxxxx or xxx- xxxx etc)
This is what i have so far:
\b1?[\s-]?\(?(?<areaCode>[2-9]\d\d)?\)?[\s-]?(? <firstThree>[2-9]\d\d)(?:\s|-)?(?<lastFour>\d{4})\b
This seems to be catching most things but it doesn't match
the first "(" part of an area code. Any ideas on how to fixthis or some number formats I may be missing?

Thanks,
Jeff
.

Nov 15 '05 #1
0 5661

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the...
5
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is...
1
by: Al Jones | last post by:
This was originally posted in the VBScript forum, several of my other questions have been vbscript related, one of the fine fellows there suggest I might want to post it as well in the jscript...
0
by: Jeff | last post by:
Hello, I'm trying to write a regular expression that will catch and group a US phone number in pretty much any format. (eg 1-xxx-xxx-xxxx or (xxx) xxx-xxxx or xxxxxxxxxx or xxx-xxxx etc) This...
4
by: Arne | last post by:
Hi, I'm trying to recognise phone numbers on the following format using regular expressions: +47 123 45 678 This should be recognised by the following regular expression: +47 \d{3} \d{2}...
2
by: Ori | last post by:
Hi, I'm looking for a good way to validate a US phone number and i though using regular expression for this. I want to support 3 different ways to enter a phone number: 1.Local Phone : 888-8899...
5
by: mac.speed | last post by:
Hi! i have set of over 100+ regular expressions, instead of checking user input against these regex i want to store all possible matches in a database and search against them. like if a regex...
1
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
3
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.