phalanx
phalanx

Reputation: 497

Regex for exactly 6 char, first must be a letter

What is the regex that matches these examples(6 characters, first is a letter, others are numbers):

u78945 - valid
s56123 - valid
456a12 - invalid
78561d - invalid
1234567 - invalid

i don't know if regular expressions are the same for every programming language. I need it for Regular Expression Validator control using VB ASP.NET.

Upvotes: 3

Views: 9072

Answers (3)

pcnThird
pcnThird

Reputation: 2372

^[a(?i)-z(?i)]\d{5}$

The (?i) code enables the expression to accept any letter without case-sensitivity. The \d{5} looks for a sequence of numbers whose length is exactly 5.

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

Use this pattern:

^[a-z][0-9]{5}$

This will match any Latin letter (lower-case unless using case-insensitive matching) followed by 5 decimal digits.

Note: You could use \d instead of [0-9], but read this for an explanation about why they are different.

Upvotes: 6

Andy G
Andy G

Reputation: 19367

[a-zA-Z]\d{5}

If you are searching explicitly from the beginning of the line use ^

^[a-zA-Z]\d{5}

and append $ for the end of the line.

Upvotes: 2

Related Questions