Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i wish to check for this format similar to this MH-9940
how do i customize regular expression to check if first 2-3 letters are capital from a-z then there should be hyphen followed by four digits from 0-9

link to good explanation about regular expression will also be helpful

here is by current code but its lacking what i want

VB
If Not Regex.IsMatch(TextBox2.Text, "^[MH-]") Then
                MsgBox("Enter proper format!!!" & vbCrLf & "Invalid Bus Number ")
                Exit Sub
Posted
Comments
Zoltán Zörgő 8-May-13 4:23am    
What was the problem with my solution so you undid the acceptance? I have answered to your question exactly as you posed it. Maybe you need to refine your question to get more accurate result.
Polarfuze 15-May-13 13:16pm    
i am sorry your solution is working fine
there must have been problem will i was typing before

Try this: ^[A-Z]{2,3}-\d{4}$
For testing an learning purposes I suggest you download Regex Coach[^].
 
Share this answer
 
Comments
Maciej Los 5-May-13 16:49pm    
Looks very good!
+5!
Polarfuze 15-May-13 13:24pm    
ur solution is working but its accepting more than 4 digits also and looks complicated
Zoltán Zörgő 15-May-13 16:17pm    
I don't understand what you mean. Please explain: this is just a note, or do you have any further problem in this topic?
Polarfuze 15-May-13 19:22pm    
your solution works fine ,my problem solved...thank you
i was talking about maciej los solution
See this pattern:
VB
^*([A-Z]{2,3}\-[0-9]{4}).*

Tested on Rad Software Regex Designer[^] and this set of records:
MH-9940
NH-1940
AGV-9Az0
FG-5940
MZ-92X0
AHV-9930

with IgnorePatternWhiteSpace and Mulitline set to TRUE.

Returns:
MH-9940
NH-1940
FG-5940
AHV-9930


Is that what you're looking for?

Many useful information you'll find here: http://www.regular-expressions.info/reference.html[^]
 
Share this answer
 
v2

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