Regex for a string that consists of 1 to 4 non-zero numeric characters or 1 to 4 non-zero numeric characters and 1 alphabet
I'm writing a regex for strings that are 1 to 4 non-zero numeric characters, or 1 to 4 non-zero numeric characters and 1 alphabet, but I'm stuck on how to fix the length of the alphabetic characters to one.
"(^[1-9]{1,4}$|^[[a-zA-Z][1-9]{1,4}]$)"
I've tried this path, but it doesn't work; it only checks for strings of 1 to 4 non-zero numeric characters.
+3
source to share
2 answers
Generally, your best chance is to check your regex with an online tool like http://www.regexr.com/ .
Also, what you are trying to achieve can be done like this: ([a-zA-Z]?[1-9]{1,4})
Explanations:
-
[a-zA-Z]
Means az alphabetic character -
?
Means 0 or 1 of the previous set (which was not in your test) -
[1-9]{1,4}
A means of 1 to 4 numeric characters as you mentioned.
0
source to share