Simple Regex

S

shapper

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
 
P

Peter Bradley

[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
 
P

Peter Bradley

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 said:
[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 said:
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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top