473,406 Members | 2,273 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,406 software developers and data experts.

Regex.IsMatch help

I need to determine whether a text box contains a value that does not convert
to a decimal. If the value does not convert to a decimal, I want to throw a
MessageBox to have the user correct the value in the text box. I have the
following code but when the user enters a decimal value the Regex.IsMatch
catches it (ex. 250.50 should be allowed, but 250.50.0 should not). My code
is as follows:

if( ! Regex.IsMatch( tboxQtyCounted.Text, "^([0-9]*)$" ) )
MessageBox.Show("Invalid Value! Please enter a valid integer or
decimal value.")

Sep 23 '05 #1
8 2956
Johnny <Jo****@discussions.microsoft.com> wrote:
I need to determine whether a text box contains a value that does not convert
to a decimal. If the value does not convert to a decimal, I want to throw a
MessageBox to have the user correct the value in the text box. I have the
following code but when the user enters a decimal value the Regex.IsMatch
catches it (ex. 250.50 should be allowed, but 250.50.0 should not). My code
is as follows:

if( ! Regex.IsMatch( tboxQtyCounted.Text, "^([0-9]*)$" ) )
MessageBox.Show("Invalid Value! Please enter a valid integer or
decimal value.")


Well, you could make it use something like [0-9]*\.+[0-9]*, but
personally I'd be tempted to go for hard-coding the test by going
through the string - it's easy to write, and easy to understand.

Unfortunately, the regex above matches "." - if you didn't mind
disallowing ".5" you could change the first * to a + to avoid this. Of
course it would be possible to improve it further, but as the
expression gets more and more complicated, it gets less and less
readable :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 24 '05 #2
Jon Skeet [C# MVP] wrote:
Well, you could make it use something like [0-9]*\.+[0-9]*, but
personally I'd be tempted to go for hard-coding the test by going
through the string - it's easy to write, and easy to understand.

Unfortunately, the regex above matches "." - if you didn't mind
disallowing ".5" you could change the first * to a + to avoid this. Of
course it would be possible to improve it further, but as the
expression gets more and more complicated, it gets less and less
readable :(


Well, I'm not going to have this discussion again :-) Just wanted to
mention a few things:

- When trying to find out whether a value will make a good decimal number, why not just try to convert it into one and see if that works? You could use the Parse or the TryParse methods on the Single, Double or Decimal structs, depending on your needs and your framework version.

- A proper expression for your purpose might look like this:

^\d*(\.\d+)?$

This matches "3", "4.7" and ".9", but not "1.2.3", ".", "7." or other
funny stuff - only decimal numbers in a common simple syntax.

Now, another thing that you miss (Jon) is the reusability aspect of
regular expressions. For example, if you wanted to find a regex for a more
complicated syntax, like the one involving exponential parts of the
number, you could just search one of the many regex libraries on the net,
like here:

http://www.regexlib.com/Search.aspx?k=decimal%20number
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Sep 25 '05 #3
Thanks to both! I am new to C# and just learning about Regular Expressions.
I appreciate Jon's concern in regards to readability because a new guy like
myself has a hard time understanding the Regular Expression syntax. In fact,
I really don't understand it very well at all. Can you point me to a source
that would teach me the essentials of the Regular Expression syntax?

When I try to implement the expression, I get an error that states -
unrecognized escape sequence. I looked at the Microsoft documentation but I
can't figure out what I'm doing wrong.

"Oliver Sturm" wrote:
Jon Skeet [C# MVP] wrote:
Well, you could make it use something like [0-9]*\.+[0-9]*, but
personally I'd be tempted to go for hard-coding the test by going
through the string - it's easy to write, and easy to understand.

Unfortunately, the regex above matches "." - if you didn't mind
disallowing ".5" you could change the first * to a + to avoid this. Of
course it would be possible to improve it further, but as the
expression gets more and more complicated, it gets less and less
readable :(


Well, I'm not going to have this discussion again :-) Just wanted to
mention a few things:

- When trying to find out whether a value will make a good decimal number, why not just try to convert it into one and see if that works? You could use the Parse or the TryParse methods on the Single, Double or Decimal structs, depending on your needs and your framework version.

- A proper expression for your purpose might look like this:

^\d*(\.\d+)?$

This matches "3", "4.7" and ".9", but not "1.2.3", ".", "7." or other
funny stuff - only decimal numbers in a common simple syntax.

Now, another thing that you miss (Jon) is the reusability aspect of
regular expressions. For example, if you wanted to find a regex for a more
complicated syntax, like the one involving exponential parts of the
number, you could just search one of the many regex libraries on the net,
like here:

http://www.regexlib.com/Search.aspx?k=decimal%20number
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Sep 26 '05 #4
I did a little more searching and found the answer to my unrecognized escape
sequence (i.e. @). Anyway, I would really appreciate some direction as to an
educational source on RegEx. Thanks!!!

"Oliver Sturm" wrote:
Jon Skeet [C# MVP] wrote:
Well, you could make it use something like [0-9]*\.+[0-9]*, but
personally I'd be tempted to go for hard-coding the test by going
through the string - it's easy to write, and easy to understand.

Unfortunately, the regex above matches "." - if you didn't mind
disallowing ".5" you could change the first * to a + to avoid this. Of
course it would be possible to improve it further, but as the
expression gets more and more complicated, it gets less and less
readable :(


Well, I'm not going to have this discussion again :-) Just wanted to
mention a few things:

- When trying to find out whether a value will make a good decimal number, why not just try to convert it into one and see if that works? You could use the Parse or the TryParse methods on the Single, Double or Decimal structs, depending on your needs and your framework version.

- A proper expression for your purpose might look like this:

^\d*(\.\d+)?$

This matches "3", "4.7" and ".9", but not "1.2.3", ".", "7." or other
funny stuff - only decimal numbers in a common simple syntax.

Now, another thing that you miss (Jon) is the reusability aspect of
regular expressions. For example, if you wanted to find a regex for a more
complicated syntax, like the one involving exponential parts of the
number, you could just search one of the many regex libraries on the net,
like here:

http://www.regexlib.com/Search.aspx?k=decimal%20number
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Sep 26 '05 #5
Oliver Sturm <ol****@sturmnet.org> wrote:
Well, you could make it use something like [0-9]*\.+[0-9]*, but
personally I'd be tempted to go for hard-coding the test by going
through the string - it's easy to write, and easy to understand.

Unfortunately, the regex above matches "." - if you didn't mind
disallowing ".5" you could change the first * to a + to avoid this. Of
course it would be possible to improve it further, but as the
expression gets more and more complicated, it gets less and less
readable :(
Well, I'm not going to have this discussion again :-) Just wanted to
mention a few things:

- When trying to find out whether a value will make a good decimal
number, why not just try to convert it into one and see if that
works? You could use the Parse or the TryParse methods on the Single,
Double or Decimal structs, depending on your needs and your framework
version.


Yes, as there's TryParse, that's a good thing to use (it also means you
don't need to worry about allowing negatives - something we haven't
mentioned yet).

Again, a simpler solution than using regular expressions :)

If it has to be *really* fast (i.e. it's definitely an application
bottleneck) a hard-coded check is much faster (at least last time I did
some benchmarking) but for actual user input, efficiency probably
wasn't an issue.
- A proper expression for your purpose might look like this:

^\d*(\.\d+)?$

This matches "3", "4.7" and ".9", but not "1.2.3", ".", "7." or other
funny stuff - only decimal numbers in a common simple syntax.
I was assuming that "7." would be allowed (as it is by Double.Parse).
The OP really needs to work out exactly what he wants to allow :)
Now, another thing that you miss (Jon) is the reusability aspect of
regular expressions. For example, if you wanted to find a regex for a more
complicated syntax, like the one involving exponential parts of the
number, you could just search one of the many regex libraries on the net,
like here:

http://www.regexlib.com/Search.aspx?k=decimal%20number


That's true. Of course, you have to make sure you use the right dialect
of regular expressions - expressions that will work in .NET may not
work in Java and vice versa. (As far as I can see, that site doesn't
say which dialect any particular expression is designed for.)

Of course, the hard-coded check would also be reusable...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '05 #6
Jon Skeet [C# MVP] wrote:
- When trying to find out whether a value will make a good decimal
number, why not just try to convert it into one and see if that
works? You could use the Parse or the TryParse methods on the Single,
Double or Decimal structs, depending on your needs and your framework
version.
Yes, as there's TryParse, that's a good thing to use (it also means you
don't need to worry about allowing negatives - something we haven't
mentioned yet).

Again, a simpler solution than using regular expressions :)

If it has to be really fast (i.e. it's definitely an application
bottleneck) a hard-coded check is much faster (at least last time I did
some benchmarking) but for actual user input, efficiency probably
wasn't an issue.


I guess the speed difference could be partly due to the fact that you'd
code your own check more along the lines of your actual requirements,
while the Parse/TryParse methods include support for several formats, plus
culture specific things. Or were you saying they are badly coded?

- A proper expression for your purpose might look like this:

^\d*(\.\d+)?$

This matches "3", "4.7" and ".9", but not "1.2.3", ".", "7." or other
funny stuff - only decimal numbers in a common simple syntax.


I was assuming that "7." would be allowed (as it is by Double.Parse).
The OP really needs to work out exactly what he wants to allow :)


True. The "7." syntax doesn't add anything to the functionality though, so
I wouldn't see the purpose of supporting it.
Now, another thing that you miss (Jon) is the reusability aspect of
regular expressions. For example, if you wanted to find a regex for a more
complicated syntax, like the one involving exponential parts of the
number, you could just search one of the many regex libraries on the net,
like here:

http://www.regexlib.com/Search.aspx?k=decimal%20number


That's true. Of course, you have to make sure you use the right dialect
of regular expressions - expressions that will work in .NET may not
work in Java and vice versa. (As far as I can see, that site doesn't
say which dialect any particular expression is designed for.)


This is usually only a problem with rather complicated stuff like zero
width assertions and similar things, where the syntax varies over
platforms. Actually, there's a POSIX standard for regular expressions -
but it's verbose and ugly, so it's not nice writing expressions to conform
with it.

When looking at such a library, it's probably best to find out what people
are focused on, so you can guess for which platform they're writing their
expressions. For example, if they're advertising Regulator on the front
page, .NET isn't too wild a guess ;-)
Of course, the hard-coded check would also be reusable...


I'd rather give someone a good regex - the problem of passing around code
snippets that do these kinds of checks is not the passing around in
itself. It's more the making small changes to adapt to the target
application that quickly breaks things.

Of course regexes break, too. But it's easy to post to a newsgroup with
your regex and a few sample inputs and get a good reply quickly. I like to
read posts like that, because they tend to be short and to the point. It's
much harder posting a complete code snippet with sample inputs in the
first place, and it's much harder to read such a post and help that person.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Sep 26 '05 #7
Johnny wrote:
I did a little more searching and found the answer to my unrecognized
escape
sequence (i.e. @). Anyway, I would really appreciate some direction as to
an
educational source on RegEx. Thanks!!!


I recently saw this: http://www.regular-expressions.info/tutorial.html

They are a little bit biased towards RegexBuddy as a tool while there are
lots of other tools around (I use Regulator although it has a few bugs in
the editor), but the content of the tutorial looks good. Just search
Google for more stuff - regular expressions have been around for about
fifty years, there's enough information available.

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Sep 26 '05 #8
Oliver Sturm <ol****@sturmnet.org> wrote:
If it has to be really fast (i.e. it's definitely an application
bottleneck) a hard-coded check is much faster (at least last time I did
some benchmarking) but for actual user input, efficiency probably
wasn't an issue.
I guess the speed difference could be partly due to the fact that you'd
code your own check more along the lines of your actual requirements,
while the Parse/TryParse methods include support for several formats, plus
culture specific things. Or were you saying they are badly coded?


I suspect it's that TryParse tries to parse it as it goes, whereas the
hard-coded check *only* checked for validity - so no time is wasted
doing actual parsing of invalid data.

I wouldn't like to say for sure though - all I have to go on is my
benchmarks from a while ago. Things might have improved in .NET 2.0 of
course.
I was assuming that "7." would be allowed (as it is by Double.Parse).
The OP really needs to work out exactly what he wants to allow :)


True. The "7." syntax doesn't add anything to the functionality though, so
I wouldn't see the purpose of supporting it.


I guess it depends what the OP wants. It's not terribly hard to support
"7." in either solution though - it's not a significant issue IMO.
That's true. Of course, you have to make sure you use the right dialect
of regular expressions - expressions that will work in .NET may not
work in Java and vice versa. (As far as I can see, that site doesn't
say which dialect any particular expression is designed for.)


This is usually only a problem with rather complicated stuff like zero
width assertions and similar things, where the syntax varies over
platforms.


True. For anything even slightly complicated I'd want to pick it apart
though, and check that everything really would work in the appropriate
platform.
Actually, there's a POSIX standard for regular expressions -
but it's verbose and ugly, so it's not nice writing expressions to conform
with it.
And in particular I doubt that either the .NET or Java implementation
is conformant :)
When looking at such a library, it's probably best to find out what people
are focused on, so you can guess for which platform they're writing their
expressions. For example, if they're advertising Regulator on the front
page, .NET isn't too wild a guess ;-)
Well, that only shows what the *owner* of the site is focused on -
there's nothing to say that the people submitting regular expressions
are working on that platform...
Of course, the hard-coded check would also be reusable...


I'd rather give someone a good regex - the problem of passing around code
snippets that do these kinds of checks is not the passing around in
itself. It's more the making small changes to adapt to the target
application that quickly breaks things.


At that point you're not really reusing with either form though.
Of course regexes break, too. But it's easy to post to a newsgroup with
your regex and a few sample inputs and get a good reply quickly.
And bad replies too, of course. It's quite easy to have something
subtle broken in a regular expression that can fool people - I've seen
it happen.
I like to
read posts like that, because they tend to be short and to the point. It's
much harder posting a complete code snippet with sample inputs in the
first place, and it's much harder to read such a post and help that
person.


If the code snippet gets long enough that it's difficult to post, I
suspect that regular expressions would usually win on the simplicity
front anyway though. As I've said several times, I'm not against
regular expressions in principle - just against using them in all
situations. This one is a border-line case for me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '05 #9

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

Similar topics

8
by: Bibe | last post by:
I've been trying to get this going for awhile now, and need help. I've done a regex object, and when I use IsMatch, it's behavior is quite weird. I am trying to use Regex to make sure that a...
2
by: Byron | last post by:
All I am trying to do is use Regex.IsMatch to validate that a string is either empty, or exactly 4 characters (digits) in length. In other words, the strings, "", and "1234" should pass, whereas...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
1
by: larry | last post by:
I'm having trouble with a pattern match expression using regex. I need to have the first 4 characters as letters and the next 2 characters as digits. ex... PROJ12 - when trying "\D{4}\d{2}"...
5
by: Dennis | last post by:
Should not the following return False if s="255xxxxyyy"? It seems to return True. Regex.IsMatch(s, "") -- Dennis in Houston
4
by: ad | last post by:
I am useing VS2005 to develop wep application. I use a RegularExpress both in RegularExpressionValidator and Regex class to validate a value. The RegularExpress is 20|\-9|\-1|?\d{1} When I...
0
by: Tidane | last post by:
Visual Basic.NET Framework 2.0 I've created a program to parse out text as the program recieved it and use Regex matching to decide what should be done. My problem is that the text is matching when...
8
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
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} | \d{2}\d | \d\d{2} |...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.