Chinmay235
Chinmay235

Reputation: 3414

Regular Expression in PHP

How to give regular expression following numbers -

(state code 2 chars)-(sub code two digits)-(sub code 1 or 2 chars)-(number max 4 digits)

AP-05-BS-9853
KL-35-AC-638
OD-03-C-3843

I have tried this way. But not working

^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{1,4}$

Upvotes: 1

Views: 59

Answers (2)

user6783030
user6783030

Reputation:

use group so that it will recognize it properly.

([a-zA-Z]{2}-\d{1,2}-[a-zA-Z]{1,2}-\d{3,4})

this is working 100%

demo : https://regex101.com/r/zZ3eT7/2

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Try this regex:

^\w{2}-\d{2}-\w{1,2}-\d{1,4}$

Demo here:

Regex101

Upvotes: 2

Related Questions