Simple Regex

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
[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
 
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
 
Back
Top