Click here to Skip to main content
12,294,775 members (57,171 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi,

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 :

a1111-111111,11a11-111111,11111-111a11,11111-11111a
Posted 6-Sep-12 0:57am
Comments
Volynsky Alex 6-Sep-12 7:41am
   
See also it:
http://www.hongkiat.com/blog/regular-expression-tools-resources/
http://rubular.com/
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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]).

Cheers
Andi
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

The following regular expression pattern can be used for this purpose.

(?=^[a-zA-Z0-9]{5}-[a-zA-Z0-9]{6}$)(?=(?:\d*[a-zA-Z]?\d*-\d*|\d*-\d*[a-zA-Z]?\d*)).*

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
http://regexhero.net/tester/[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi Rashmi
it may be help you...
[0-9]{0,6}[-]{1}[0-9]{0,7}[a-zA-Z]{0,1}



Thank u
Gaja
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

See below article you will find solution yourself

The 30 Minute Regex Tutorial[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
http://www.hongkiat.com/blog/regular-expression-tools-resources/[^]
  Permalink  
Comments
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!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 7 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100