Zahid Mustafa
Zahid Mustafa

Reputation: 185

Regex to Allow 1st letter of a string only Alphabet in C#

I have a case where i need to allow Alphanumeric but 1st letter of the string should always be Alphabet only

Below is sample case

string text1 = "A1222"; Valid
string text2 = "1A22"; // Invalid

Upvotes: 0

Views: 69

Answers (1)

Cleptus
Cleptus

Reputation: 3541

A variant of what was already said in the comments would be

Regex reg = new Regex(@"^[a-z][a-z\d]*$", RegexOptions.IgnoreCase);

That one is not particularly complex, but for complex ones it is very nice making them case insensitive.

Warning: Do note that [A-z] would yield wrong results as mentioned in this related question Difference between regex [A-z] and [a-zA-Z]

Upvotes: 1

Related Questions