Click here to Skip to main content
15,905,148 members
Please Sign up or sign in to vote.
4.86/5 (4 votes)

I need a regular expression where it should in pattern of 5digits-6digits, in between these digits I need to enter 0 or 1 occurrence of alphabets. Please help me.
examples like :

Volynsky Alex 6-Sep-12 7:41am    
See also it:

Regular expression, or often known as regex, is a pattern that consist of rules used to match a certain set of strings. They are extremely powerful, and you’ll need them in most programming languages you come across, especially when there’s a need to scan and match context for further actions.

Here’s a 30 Useful Tools & Resources, I hope it will help you:[^]
Share this answer
Rashmi R 6-Sep-12 7:51am    
Thank you for your help, i will go through the links
Volynsky Alex 6-Sep-12 8:30am    
:) You are welcome!
See below article you will find solution yourself

The 30 Minute Regex Tutorial[^]
Share this answer
You should make an effort to learn more about Regular Expressions. A good place to get started is here. Another very helpful site is regexlib[^]; they have a large library of Regular Expressions. They also have a RegEx tester[^]. Also a very useful RegEx tool is available here.
Share this answer
Hi Rashmi
it may be help you...

Thank u
Share this answer
The following regular expression pattern can be used for this purpose.


In this pattern the (?=^[a-zA-Z0-9]{5}-[a-zA-Z0-9]{6}$) will look ahead to sea that the string contains 5-6 alphanumeric digits.

Then (?=(?:\d*[a-zA-Z]?\d*-\d*|\d*-\d*[a-zA-Z]?\d*)) looks ahead to see if there is either one or none alphabet in the string. If the above two tests succeed then the string is matched.

It can be tested here[^]
Share this answer
Guessing from your question, I unerstand that you have groups of 5 to 6 digits, plus mixed in some single a-z characters (none or one per group).

If you have to only take these groups that match your constraints (at most one [a-z] in a group), then I have the following solution:
string pattern = @"\b[0-9][0-9][0-9][0-9][0-9][0-9a-z]?\b"
              + @"|\b[a-z][0-9][0-9][0-9][0-9][0-9]?\b"
              + @"|\b[0-9][a-z][0-9][0-9][0-9][0-9]?\b"
              + @"|\b[0-9][0-9][a-z][0-9][0-9][0-9]?\b"
              + @"|\b[0-9][0-9][0-9][a-z][0-9][0-9]?\b"
              + @"|\b[0-9][0-9][0-9][0-9][a-z][0-9]?\b"

This may look silly, but I don't come up with a better solution for the above stated problem. The issue is that you can not count how often a pattern was matched before (e.g. the [a-z]).

Share this answer

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