Glory Raj
Glory Raj

Reputation: 17701

Regex for serial number matching 2 letters and 7 numbers

Is there any regex for validating a serial number input, the range should be between DC0001000 to DC9999999.

I searched through the internet but I didn't find any solution for this validating serial number regex.

The Prefix DC is mandatory and the next 7 digits must be in the range (0001000 - 9999999).

I tried with this regex -

[DC]{1}\d{7}[0001000-9999999]

but it didn't work for me.

Is there a regex that will match this?

Upvotes: 0

Views: 1573

Answers (4)

Ashokk
Ashokk

Reputation: 11

Hi try this for RegExp beginers

([D]{1}[C]{1}[0]{3})?([D]{1}[C]{1}[0]{3}[1-9]{1}[0-9]{3})|([D]{1}[C]{1}[1-9]{1})?([D]{1}[C]{1}[1-9]{1}[0-9]{6})|([D]{1}[C]{1}[0-9]{1}[1-9]{1})?([D]{1}[C]{1}[0-9]{1}[1-9]{1}[0-9]{5})|([D]{1}[C]{1}[0-9]{2}[1]{1})?([D]{1}[C]{1}[0-9]{2}[1]{1}[0-9]{4})

Upvotes: -1

SamWhan
SamWhan

Reputation: 8332

This should do it:

^DC(?=\d{0,3}[1-9])\d{7}$

It checks that the string starts with DC and then a positive look ahead checks that one of the following four digits isn't zero - and then 7 digits.

Check it out here at regex101.

Edit simplified (removed the unnecessary first test)

Upvotes: 2

Rawling
Rawling

Reputation: 50114

If we're going for the shortest one, how about this?

DC(?!0000)\d{7}

Upvotes: 7

Srinivasan Thoyyeti
Srinivasan Thoyyeti

Reputation: 63

you are looking look ahead regular expressions. These expressions wont change reg ex pointer. so that pointer can remain at -1 and you can visit the entire string "n" of times. ?=(expression1)?=(expression2)...?=(expression_n)

Also useful in password policy verification.

Upvotes: -1

Related Questions