473,378 Members | 1,457 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,378 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 5663

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.