473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 3494
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Daniel Billingsley <db**********@NO.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om... Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #9
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Ron,

A couple of thoughts.

This is the sort of expression that lends itself quite well to brute-force
testing. It's pretty easy for you to throw a whole mess of values at it
(even the whole Int32 range) to see how it behaves.

My general approach is not to do this kind of thing in regex, as it's so
tough to read and debug. It's much easier to do it in code.

If you do, here's my advice:

1) Download my regex tool from here:

http://www.gotdotnet.com/Community/U...1-4EE2729D7322

2) Write your regexes like:

string pattern = @"
(
\d | # any single digit number
\d\d | # any two-digit number
1[01]\d # 10d or 11d
12[0-8] # 120 - 128
)
"

You'll need to use RegexOptions.IgnorePatternWhitespace

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Ron Rohrssen" <rl******@fightspam.rockwellcollins.com> wrote in message
news:eY*************@TK2MSFTNGP11.phx.gbl...
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #11
I also found a tool called "The Regex Coach" which was invaluable. It has a
feature to "step" through the evaluation so you can see exactly when things
match or go haywire. It's a standard regex tool and doesn't support all the
features of the .NET implementation.

"Eric Gunnerson [MS]" <er****@online.microsoft.com> wrote in message
news:OJ**************@TK2MSFTNGP12.phx.gbl...
Ron,

A couple of thoughts.

This is the sort of expression that lends itself quite well to brute-force
testing. It's pretty easy for you to throw a whole mess of values at it
(even the whole Int32 range) to see how it behaves.

My general approach is not to do this kind of thing in regex, as it's so
tough to read and debug. It's much easier to do it in code.

If you do, here's my advice:

1) Download my regex tool from here:

http://www.gotdotnet.com/Community/U...1-4EE2729D7322
2) Write your regexes like:

string pattern = @"
(
\d | # any single digit number
\d\d | # any two-digit number
1[01]\d # 10d or 11d
12[0-8] # 120 - 128
)
"

You'll need to use RegexOptions.IgnorePatternWhitespace

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "Ron Rohrssen" <rl******@fightspam.rockwellcollins.com> wrote in message
news:eY*************@TK2MSFTNGP11.phx.gbl...
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ron Rohrssen <rl******@fightspam.rockwellcollins.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #12

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

Similar topics

3
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)...
5
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,...
10
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...
18
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
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...
7
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 -...
7
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...
6
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...
3
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...
25
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.