Triandh Velchuri Triandh Velchuri - 1 month ago 21
C# Question

Regular expression value

I had written the below regular expression

string validnumber = @"^[a-zA-Z]{2}\d{7}(?:-?[a-zA-Z0-9]{1,4})?$";


This will allow the numbers like "AA1234567" and "AA1234567-12345".

I want to allow below all formats

? = Only Alphabets/ Only Numeric / Alphanumeric

AA1234567?
AA1234567??
AA1234567???
AA1234567????

AA1234567-?
AA1234567-??
AA1234567-???
AA1234567-????

Answer

I would say

string validnumber = @"^[a-zA-Z]{2}\d{7}(-?[a-zA-Z0-9]{1,4})$";