jaya k
jaya k

Reputation: 47

regular expression to identify certain number pattern

I want to build a regular expression to identify certain number pattern

The expressions required would be:

1) 6 numbers, starting with 00

2) 6 numbers, starting with 01

3) 8 numbers, starting with 200.

I started with ^\d{0,6}(.\d{00})?$ bit it did not work

How can it be done?

Upvotes: 1

Views: 103

Answers (3)

KyleFairns
KyleFairns

Reputation: 3057

Try this:

^(0[01][0-9]{4}|200[0-9]{5})$

Will match 0 followed by a 0 or 1 followed by 4 numbers 0-9 (total 6 digits), or it will match 200 followed by 5 digits (total 8 digits)

(Using character groups, due to the fact that the language was not specified, therefore, whether the special characters need extra escapes is unknown)

Upvotes: 1

Lance Toth
Lance Toth

Reputation: 440

How about this: a number starting with either 200 and a number, 00 or 01 and 4 numbers

(200\d|00|01)\d{4}

Upvotes: 0

meisterluk
meisterluk

Reputation: 824

I think you are looking for the alternation operator |. It takes either the pattern to the left or the pattern to the right. Hence, you end up with the following regular expression:

^(00\d{4}|01\d{4}|200\d{5})$

Upvotes: 0

Related Questions