12,294,775 members (57,171 online)
Rate this:
See more:
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
Volynsky Alex 6-Sep-12 7:41am

http://www.hongkiat.com/blog/regular-expression-tools-resources/
http://rubular.com/

Rate this:

## 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
v4
Rate this:

## 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/[^]
Rate this:

## Solution 4

Hi Rashmi
[0-9]{0,6}[-]{1}[0-9]{0,7}[a-zA-Z]{0,1}

Thank u
Gaja
Rate this:

## 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.
v2
Rate this:

## Solution 2

See below article you will find solution yourself

The 30 Minute Regex Tutorial[^]
Rate this:

## 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/[^]
Rashmi R 6-Sep-12 7:51am

Volynsky Alex 6-Sep-12 8:30am

:) You are welcome!

Top Experts
Last 24hrsThis month
 OriginalGriff 360 CHill60 330 Richard MacCutchan 305 Karthik Bangalore 254 Dave Kreskowiak 224
 OriginalGriff 8,795 ppolymorphe 5,362 CHill60 5,260 Karthik Bangalore 4,576 Sergey Alexandrovich Kryukov 4,476