I’ve been struggling to figure out how to best do this regular expression.
Here are my requirements:
- Up to 8 characters
- Can only be alphanumeric
- Can only contain up to three alpha characters [a-z] (zero alpha characters are valid to)
Any ideas would be appreciated.
This is what I’ve got so far, but it only looks for contiguous letter characters:
^(\d|([A-Za-z])(?!([A-Za-z]{3,}))){0,8}$
I’d write it like this:
It has two parts:
(?=[a-z0-9]{0,8}$)(?:\d*[a-z]){0,3}\d*$[a-z]among\d*Rubular
On rubular.com
Alternatives
I do think regex is the best solution for this, but since the string is short, it would be a lot more readable to do this in two steps as follows:
[a-z0-9]{0,8}\d<= 3