Regex for 0001 to 9999

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
Sergey Poberezovskiy said:
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}$
 
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 said:
Sergey Poberezovskiy said:
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}$
 
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
 
* 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 said:
Sergey Poberezovskiy said:
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}$
 
^\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
 
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.
 
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 said:
Sergey Poberezovskiy said:
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}$
 
Back
Top