471,355 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Regex for 0001 to 9999

Hi,

I need to validate a textbox to have exactly 4 characters that represent the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?

Jun 29 '07 #1
8 6943

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:0B**********************************@microsof t.com...
Hi,

I need to validate a textbox to have exactly 4 characters that represent
the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?
A negative lookahead:

^(?!0{4})[0-9]{4}$

Jun 29 '07 #2
Ben,

Something seems to be wrong with your solution - it does not seem to match
any number in the range 0001 to 9999 (I have only tried a few dozen) :-(

"Ben Voigt [C++ MVP]" wrote:
>
"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:0B**********************************@microsof t.com...
Hi,

I need to validate a textbox to have exactly 4 characters that represent
the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?

A negative lookahead:

^(?!0{4})[0-9]{4}$
Jun 29 '07 #3
On Jun 29, 8:40 am, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
Something seems to be wrong with your solution - it does not seem to match
any number in the range 0001 to 9999 (I have only tried a few dozen) :-(
It looks okay to me. Here's a sample program to demonstrate it:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
Regex regex = new Regex("^(?!0{4})[0-9]{4}$");
Console.WriteLine (regex.IsMatch("0000"));
Console.WriteLine (regex.IsMatch("0001"));
Console.WriteLine (regex.IsMatch("9999"));
Console.WriteLine (regex.IsMatch("1234"));
Console.WriteLine (regex.IsMatch("123456"));
}
}

As desired, this writes out False, True, True, True, True, False.

Jon

Jun 29 '07 #4
* Sergey Poberezovskiy wrote, On 29-6-2007 9:40:
Ben,

Something seems to be wrong with your solution - it does not seem to match
any number in the range 0001 to 9999 (I have only tried a few dozen) :-(
If it's being used in a ClientSide regex validator it won't work.
Javascript does not support look arounds.

So the original solution seems to be the only correct option if you use
regex alone. If you're using ASP.NET validators though, I'd use a regex
validator to enforce the format (e.g. [0-9]{4}) and a range validator to
enforce the range from 1 to 9999. That way you can change formats much
easier.

Jesse
>
"Ben Voigt [C++ MVP]" wrote:
>"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:0B**********************************@microsof t.com...
>>Hi,

I need to validate a textbox to have exactly 4 characters that represent
the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?
A negative lookahead:

^(?!0{4})[0-9]{4}$
Jun 29 '07 #5
^\d\d\d\d$

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:0B**********************************@microsof t.com...
Hi,

I need to validate a textbox to have exactly 4 characters that represent
the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?

Jun 29 '07 #6
Kevin Spencer wrote:
^\d\d\d\d$
Not quite. This one will also recognize 0000:
>I need to validate a textbox to have exactly 4 characters that
represent the number 0001 to 9999 (cannot be 0000).
Ebbe
Jun 29 '07 #7
On Jun 29, 5:34 am, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
Hi,

I need to validate a textbox to have exactly 4 characters that represent the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?
Hi!

I think this is the minimal, deterministic regular expression for your
problem:
"000[1-9] | 00[1-9]\d | 0[1-9]\d{2} | [1-9]\d{3}"

It only differs from yours in the leading digits: they're changed to
zeroes to make the expression deterministic (this should be a good
idea, but it's not obligatory).
Rgrds.

--
Adam Visegradi
Jun 30 '07 #8
Ben,

not sure what happened last time - must have copied incorrectly - it does
work - thank you.

Jesse - seems to be working just fine in XP/VS2005/IE6.0

Adam - thank you for your suggestion.

"Ben Voigt [C++ MVP]" wrote:
>
"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:0B**********************************@microsof t.com...
Hi,

I need to validate a textbox to have exactly 4 characters that represent
the
number 0001 to 9999 (cannot be 0000).
The regext I came up with rather long:
^(\d){3}[1-9] | \d{2}[1-9]\d | \d[1-9]\d{2} | [1-9]\d{3}$

and not easilily extendable should the need arise for say 5 or 6 numbered
text.

Can anyone suggest a better solution?

A negative lookahead:

^(?!0{4})[0-9]{4}$
Jul 2 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by smcgouga | last post: by
10 posts views Thread by Chance Hopkins | last post: by
2 posts views Thread by Johannes Hammersen | last post: by
15 posts views Thread by Kay Schluehr | last post: by
4 posts views Thread by =?Utf-8?B?VGVycnk=?= | last post: by
3 posts views Thread by William Gill | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.