Click here to Skip to main content
15,886,821 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
i want to match anyone of below possibilities through a regular expression validator for a textbox
the user entry would be matched from these 3 patterns anyone of them would be considered valid

1. All four numbers but starting with digit 2 e.g 2000, 2300 etc
2. first four numbers starting with 2 and last one alpha bets between A-H e.g 2000C,2310B,2210G
3. first four numbers starting with 2 and last one alpha bets between A-H e.g 2000AC,2310GB,2000HD

what could be the possible regular expression of above scenario?
currently i am using: ([0-9]{4})|([0-9]{4}[A-H]{1})|([0-9]{4}[A-H]{2})
it matches only the first possibility i.e ([0-9]{4})on other two it is not working
Posted
Comments
Prasad Khandekar 6-May-13 3:28am    
Try with this expression [0-9]{4}[A-H]{0,2}. It matches all possible cases. For regular expressions there is an excellent tool call Expresso. Grab it from here (http://www.ultrapico.com)

Regards,
Sergey Alexandrovich Kryukov 6-May-13 3:29am    
Exactly. You could make it a formal answer; will you?
—SA
Prasad Khandekar 6-May-13 3:36am    
Yes Sir,

Will do it immediately.

Regards,
Sergey Alexandrovich Kryukov 6-May-13 3:29am    
"Not working" is not informative...
—SA

Solution 1 is correct, but please note that [0-9]{4} is incorrect for the first criteria. [0-9]{4} also matches a value like 1000.
The regular expression for a four digit numbers starting with the digit 2 is 2[0-9]{3}.

So the final expression would be
2[0-9]{3}[A-H]{0,2}
 
Share this answer
 
Comments
Prasad Khandekar 6-May-13 3:51am    
Oops, You are absolutely right. Thank's for pointing out the mistake. Please downvote my solution for proving the wrong answer.
Sergey Alexandrovich Kryukov 6-May-13 10:33am    
5ed.
—SA
Hello,

Try with this expression
^(2[0-9]{3}[A-H]{0,2})$    // as partly suggested by (André Kraak)
It matches all possible cases.

For regular expressions there is an excellent tool called Expresso^]. Grab it.

Regards,


[Edit: Using ^ and $ checks that the whole line contains only what you are looking for. Otherwise you'd get a match on anything with extra characters.
E.g. these would match:
"somerandomchars2000AC" if you don't include ^
"2000ACxyxblahandthing" if you don't include $
"somemore2000ACrandomchars" if you don't include either.

Also, I've put the expression in brackets to create a group, as per your question, allowing you to use group syntax to obtain the matched string ($1).
Regards,
Ian.]
 
Share this answer
 
v3
Comments
André Kraak 6-May-13 3:45am    
My 5.
Qadri Jillani 6-May-13 6:00am    
thanx for guidance but please provide regular expression for the rest of 2 patterns also.
Regards: Qadri Jillani
Prasad Khandekar 6-May-13 6:11am    
The provided regular expression will capture the remaining patterns as well. try it with Expresso.
Sergey Alexandrovich Kryukov 6-May-13 10:33am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900