Regular Expression Help

T

tdavisjr

Anyone can give me a quick RegEx pattern that would match a 7 or 10
digit number. This is not a range; but exactly 7 or 10. I'm beating
my head over this, nothing I am doing works. Thanks
 
K

Karl Seguin [MVP]

I believe this works:

^\d{7}(\d{3})?$

it makes the last 3 digits optional (as a group)

Karl
 
T

Tod Birdsall, MCSD for .NET

Hi,

You will probably find RegExlib.com to be very helpful. It has a
library of custome regular expressions. There is also a tool that
allows you to test your own regular expressions.
 
T

tdavisjr

Thanks Karl. That seem to work. I probably need to adjust my thinking
when doing RegEx's. I was trying to do something like this: (\d{7}) |
(d{10}) when it was either or. I never thought about making the last
part optional as a group.

And Tod, Thanks for the reference. I did visit this site and used its
tester, however, I searched the database and I could find a simple
example for what I wanted.
 
K

Karl Seguin [MVP]

That works too, you just have your parenthesis in the wrong place:

^(\d{7}|\d{10})$

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top