473,795 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7669

"Sergey Poberezovskiy" <Se************ *****@discussio ns.microsoft.co mwrote
in message news:0B******** *************** ***********@mic rosoft.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************ *****@discussio ns.microsoft.co mwrote
in message news:0B******** *************** ***********@mic rosoft.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
<SergeyPoberezo vs...@discussio ns.microsoft.co mwrote:
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.Reg ularExpressions ;

class Test
{
static void Main()
{
Regex regex = new Regex("^(?!0{4} )[0-9]{4}$");
Console.WriteLi ne (regex.IsMatch( "0000"));
Console.WriteLi ne (regex.IsMatch( "0001"));
Console.WriteLi ne (regex.IsMatch( "9999"));
Console.WriteLi ne (regex.IsMatch( "1234"));
Console.WriteLi ne (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************ *****@discussio ns.microsoft.co mwrote
in message news:0B******** *************** ***********@mic rosoft.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************ *****@discussio ns.microsoft.co mwrote
in message news:0B******** *************** ***********@mic rosoft.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
<SergeyPoberezo vs...@discussio ns.microsoft.co mwrote:
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************ *****@discussio ns.microsoft.co mwrote
in message news:0B******** *************** ***********@mic rosoft.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5126
by: smcgouga | last post by:
Visual Basic 6. ADO 2.8 I have an as400 DB2 V5R1 datasource. Dates are defined as *ISO format and have a range from '0001-01-01' to '9999-12-31'. I am trying to update a date field on the database with a value of '0001-01-01' (*LOVAL) The problem is that I need to use ADO cursors and can not use the SQL update command: "update tbl set dateField = '0001-01-01 where ..." (note this works no problem)
10
3092
by: Chance Hopkins | last post by:
I'm trying to match a set of matches after some initial text: mytext: "something" "somethingelse" "another thing" "maybe another" (?:mytext: )(?<mymatch>{1,1}+{1,1}+)+ I only get the last one "maybe another". I want to get all the values with quotes as a group, hence the + after the ()'s. Is this possible?
2
2887
by: Johannes Hammersen | last post by:
Hi, when I try to set the Minimum Date on a RangeValidator to 01.01.0001 I get an error, that 01.01.0001 can not be converted to a date. But the DateType can be 01.01.0001. Convert.ToDateTime("01.01.0001") works. So why can't I set the Minimum of the RangeValidator to that Date? I have the same Problem with the Maximun Value. If I try to set that let's say to the 31th December 9999 it will give me an error. You can reproduce this...
15
3237
by: Kay Schluehr | last post by:
I have a list of strings ls = and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in . There might be relations between those strings: s_k.startswith(s_1) -> True or s_k.endswith(s_1) -> True. An extreme case would be ls = . For this reason SRE_Match should provide the longest possible match. Is there a Python module able to create an optimized regex rx...
1
2625
Cyberdyne
by: Cyberdyne | last post by:
Hi Guys I am working on a database that will have a locked auto field with the following characteristics. It has to be a number that will be stored in a table, it needs to have the following format 00-0000 , in the actual table it will start with 06-0001 that being 06 is the year then - 0001 being the first case, from then on it will be 06-0002, 06-0003, 06-0004 and so on when next year hits I want the 06 to change to 07-0001 and so on. The...
8
10289
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a bunch of hits with this - I'm only interested in the *last* one. Is there a way to build the concept "last" into the
2
1302
by: stanleytweedle | last post by:
hi. the "thread" i liked (which broght me here from google) wasn't a thread at all? but located here: http://www.thescripts.com/forum/thread493095.html my app could go any direction at its current infantile stages, so it's wide open for suggestion i want to let someone enter currency value from $0.01 (exaggeration) to $9999.99. i'm just trying to make it easier on myself for the db backend... do i want to let this guy enter text field?...
4
4091
by: =?Utf-8?B?VGVycnk=?= | last post by:
Converting an old application and came accross a funny problem. Have a subscription DB with a renewal date field. If the subscription is considered a 'freebee', then it never has to be renewed and the renewal date is null. Well, discovered that nullableof(Date) does not work well with the datetimepicker nor with Crystal Reports which won't even recognize that type. Decided to change these date to 12/31/9999 (which is close enough to never...
3
283
by: William Gill | last post by:
I am not to sharp on my regular expressions because I haven't used them in quite a while. So I am relearning regex and the PHP regex functions at the same time. Which means when I screw up, I'm not sure it's the regular expression that's wrong or the specifics of the PHP function's application thereof. I have a couple of questions, since I'm basically starting from scratch should I focus on the ereg* functions instead of the preg*...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.