473,788 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression Question

Slightly off topic....

How can I write a regex that limits user input to 3 digits in the range of
1-128?

I've been trying \d{1,128} but this allows for a match on more than 3
digits.

Thanks.
Nov 15 '05 #1
11 3562
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
How can I write a regex that limits user input to 3 digits in the range of
1-128?

I've been trying \d{1,128} but this allows for a match on more than 3
digits.


Yes - that allows up to 128 digits.

If you want to limit it to 0-128, I suggest you write a simple method
that checks that (e.g. by first checking the length and that all the
characters are digits, and then using Int32.Parse). Is there some
reason you want to use a regular expression?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Checking the length and then for a max value of 128 is redundant, isn't it?

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
How can I write a regex that limits user input to 3 digits in the range of 1-128?

I've been trying \d{1,128} but this allows for a match on more than 3
digits.


Yes - that allows up to 128 digits.

If you want to limit it to 0-128, I suggest you write a simple method
that checks that (e.g. by first checking the length and that all the
characters are digits, and then using Int32.Parse). Is there some
reason you want to use a regular expression?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote:
Checking the length and then for a max value of 128 is redundant, isn't it?


Only sort of. It's not actually redundant in that it prevents strings
of the form "0123". It's also usful to do as a "first" check before the
more expensive checks (checking that each character is a digit and the
parsing itself).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
I can see that I didn't make this clear.

I'm trying to limit the user to 3 digits with a possible range of values
from 1-128.

Not 128 digits.

I'm doing it this way because the edit regex is exposed to the user through
configuration choices. So, I'll restrict them to valid regex expressions.
Later, I'll have to use this expression to validate input by another user.

I've come up with this regex,
\b(12[0-8] | 1[0-1][0-9] | [01][0-2][0-8] | [0-9][0-9] | [0-9])\b

This seems to be ok. Is there a better way?

Ron
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
How can I write a regex that limits user input to 3 digits in the range of 1-128?

I've been trying \d{1,128} but this allows for a match on more than 3
digits.


Yes - that allows up to 128 digits.

If you want to limit it to 0-128, I suggest you write a simple method
that checks that (e.g. by first checking the length and that all the
characters are digits, and then using Int32.Parse). Is there some
reason you want to use a regular expression?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
I can see that I didn't make this clear.

I'm trying to limit the user to 3 digits with a possible range of values
from 1-128.

Not 128 digits.
Indeed. The problem was that your regular expression *was* allowing 128
digits, because the \d{x,y} bit basically says "I want between x and y
digits inclusive".
I'm doing it this way because the edit regex is exposed to the user through
configuration choices. So, I'll restrict them to valid regex expressions.
Later, I'll have to use this expression to validate input by another user.

I've come up with this regex,
\b(12[0-8] | 1[0-1][0-9] | [01][0-2][0-8] | [0-9][0-9] | [0-9])\b

This seems to be ok. Is there a better way?


Well, you can make it slightly better:

\b(12[0-8] | 1[0-1][0-9] | 0?[0-9]?[0-9])\b

I *think* that works okay, but to be honest I haven't tried it.

Currently your regular expression isn't terribly clear because the
third "chunk" of it overlaps with the second chunk. (IMO, anyway.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Yes, I see the overlap now. Thanks.
\b(12[0-8] | 1[0-1][0-9] | 0?[0-9]?[0-9])\b
What does the ? mean in your regex?

Ron

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
I can see that I didn't make this clear.

I'm trying to limit the user to 3 digits with a possible range of values
from 1-128.

Not 128 digits.


Indeed. The problem was that your regular expression *was* allowing 128
digits, because the \d{x,y} bit basically says "I want between x and y
digits inclusive".
I'm doing it this way because the edit regex is exposed to the user through configuration choices. So, I'll restrict them to valid regex expressions. Later, I'll have to use this expression to validate input by another user.
I've come up with this regex,
\b(12[0-8] | 1[0-1][0-9] | [01][0-2][0-8] | [0-9][0-9] | [0-9])\b

This seems to be ok. Is there a better way?


Well, you can make it slightly better:

\b(12[0-8] | 1[0-1][0-9] | 0?[0-9]?[0-9])\b

I *think* that works okay, but to be honest I haven't tried it.

Currently your regular expression isn't terribly clear because the
third "chunk" of it overlaps with the second chunk. (IMO, anyway.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
Yes, I see the overlap now. Thanks.
\b(12[0-8] | 1[0-1][0-9] | 0?[0-9]?[0-9])\b


What does the ? mean in your regex?


It's basically "the preceding character is optional".

I really wouldn't start messing around with regular expressions like
this without reading a thorough tutorial on it and having MSDN handy,
personally.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Agreed!

I use regular expressions about once every 2 years. I still haven't found a
straight forward tutorial that doesn't take days to read through. I even
bought the O'Reilly Regular Expressions book when I was doing some Perl
several years ago. At the time I found it pretty useful. But like most
things, if you don't use it, you lose it.

Do you have any "favorite" sites or books that you like to use for
reference?

Ron
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
Yes, I see the overlap now. Thanks.
\b(12[0-8] | 1[0-1][0-9] | 0?[0-9]?[0-9])\b


What does the ? mean in your regex?


It's basically "the preceding character is optional".

I really wouldn't start messing around with regular expressions like
this without reading a thorough tutorial on it and having MSDN handy,
personally.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #9
Ron Rohrssen <rl******@fight spam.rockwellco llins.com> wrote:
Agreed!

I use regular expressions about once every 2 years. I still haven't found a
straight forward tutorial that doesn't take days to read through. I even
bought the O'Reilly Regular Expressions book when I was doing some Perl
several years ago. At the time I found it pretty useful. But like most
things, if you don't use it, you lose it.
Yup.
Do you have any "favorite" sites or books that you like to use for
reference?


I personally just use the MSDN for the most part - but then, I haven't
had to use regular expressions much outside responding to newsgroup
posts :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9746
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject) method/function, I need the second argument 'replacement' to be another regular expression ( not a string) . So when I find a 'certain kind of string' in
5
2534
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL, and then reconstruct another URL based on it. For example, I need to scan a web page looking for something like <a href="some_dir/list_20050815100225.csv">. I don't know in advance what the date/time in the file name will be. I need to take the...
10
3037
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In other words, as the regular expression (....)($) matches the "4567" in the string "1234567", how would I create a similar regular expression that only matches the "45" in the same string. The same regular expression would match "32" in the string...
18
3043
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How ??
5
3110
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address and this method does not.... Does anyone have an explanation? Function IsValidEmail(ByVal strIn As String) As Boolean
7
371
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist - Album- i have try this syntax but it failed
7
3831
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
6
2293
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not work: it seems that < is not a valid character, so the beginning of the word starts at theletter 'p' instead of '<'. Because I'm not an expert in regular expressions, maybe someone of you guys can help me? I need the correct regex to find the...
3
2566
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5169
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
0
9656
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
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
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...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.