By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,685 IT Pros & Developers. It's quick & easy.

Simple Regex

P: n/a
Hello,

I need a regex expression that allows only numbers in the following
format:

2312-986

The first number can't start with 0:
0231-123 is invalid.

However:
1231-012 is valid

Could someone help me out?

Thanks,
Miguel

Jun 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
[1-9]\d{3}-\d{3}

Ought to do it, I would think. It checks out in my RegEx checker, anyway,
using the examples you give.
Peter
"shapper" <md*****@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
Hello,

I need a regex expression that allows only numbers in the following
format:

2312-986

The first number can't start with 0:
0231-123 is invalid.

However:
1231-012 is valid

Could someone help me out?

Thanks,
Miguel

Jun 12 '07 #2

P: n/a
"shapper" <md*****@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
Could someone help me out?
[1-9][0-9][0-9][0-9]-[0-9][0-9][0-9]

I'm sure there are much shorter and cleverer ways using groups etc...
--
http://www.markrae.net

Jun 12 '07 #3

P: n/a
Sorry to reply to myself, but I miscopied my RegEx. It should, of course,
be:

^[1-9]\d{3}-\d{3}$

Since I'm assuming you want simply this format and nothing else. If you
leave the start and finish markers (^, $) out then it will match the format
anywhere in the expression.
e.g. 2222abd1234-023xxxx would match

Apologies
Peter

"Peter Bradley" <pb******@uwic.ac.ukwrote in message
news:uG**************@TK2MSFTNGP04.phx.gbl...
[1-9]\d{3}-\d{3}

Ought to do it, I would think. It checks out in my RegEx checker, anyway,
using the examples you give.
Peter
"shapper" <md*****@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
>Hello,

I need a regex expression that allows only numbers in the following
format:

2312-986

The first number can't start with 0:
0231-123 is invalid.

However:
1231-012 is valid

Could someone help me out?

Thanks,
Miguel


Jun 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.