Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

6 characters followed by hyphen(optional) followed by optional 4 characters

Tags:

regex

I need a regular expression for which:

the string is alphanumeric and have exactly 6 characters in the first half followed by hyphen(optional) followed by optional 4 characters:(cannot have more than 4 characters in the second half)

so any of the following is valid

11111A
111111-1
111111-yy
yyyyy-989
yyyyyy-9090

I thought this expression /[a-zA-Z0-9]([-])?[a-zA-Z0-9]{5,10}$/; should work but I'm unable to get it working correctly.

like image 554
user268375 Avatar asked Dec 04 '25 19:12

user268375


1 Answers

^[a-zA-Z0-9]{6}(-[A-Za-z0-9]{1,4})?$
like image 197
tanascius Avatar answered Dec 06 '25 11:12

tanascius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!