I need help forming a regular expression to check if the input string is ONLY of the pattern 1 or 2 alphabets (can be lower or uppercase) followed by 1 or 2 digits. Valid strings would be d1,d15,ha1,ha20 so on.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The following should do what you want:
[a-zA-Z]is a character class that matches any letter,\dis equivalent to[0-9]and matches any digit, and{1,2}means “repeat the previous element 1 or 2 times”.\Aand\zare anchors, and they only match at the beginning and end of a string respectively (they don’t match any characters, they just require the string to start or end at them to allow a match).You will also commonly see the anchors
^and$, I used\Aand\zbecause$will match just before a newline at the end of the string and can have its behavior modified by options, whereas\zalways means the very end of the string.The following page gives a nice summary on regular expression syntax:
http://www.regular-expressions.info/reference.html