I'm trying to write my first regular expression.. I'm having value of the following combinations
letters [upper or lowercase]
numbers [0-9]
letters in parentheses [(M)]
no spaces or other characters
a few examples:
OK: "A3"
OK: "N15"
OK: "A126"
OK: "B6469"
OK: "A57(M)"
OK: "A1(M)"
NOT OK: "TF9 3TF"
NOT OK: "B64 69"
My Regular Exp:
^(([a-zA-Z][1-9]\([a-zA-Z]\)?)|([a-zA-Z][1-9][1-9]\([a-zA-Z]\)?)|([a-zA-Z][1-9]?)|([a-zA-Z][1-9][1-9]?)|([a-zA-Z][1-9][1-9][1-9]?)|([a-zA-Z][1-9][1-9][1-9][1-9]?))$
its works fine for me. but i want best solution for it, like my regular expression seems to be too long because for each combination i make a expression and then combine all these but i want to reduce my regular expression like in my sample code contain first alphabet then number that can be one or more but up till 5. tell me how can i make one expression that will work for (A1, A12, A123, A1234, A12345)?
That does seem overly complex.
^[a-zA-Z]\d{1,5}(?:\(M\))?$
Should do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With