By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,112 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Regex Number Check

P: n/a
Should not the following return False if s="255xxxxyyy"? It seems to return
True.

Regex.IsMatch(s, "[0-9]")
--
Dennis in Houston
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]")
--
Dennis in Houston

Nov 21 '05 #2

P: n/a
?Thanks for reply. Which one should return false, the one you send
(Regex.IsMatch(s, "^\d+$") or mine (Regex.IsMatch(s, "[0-9]")? Mine returns
true in VB.net 2003.

"Dr Screwup" wrote:
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]")
--
Dennis in Houston


Nov 21 '05 #3

P: n/a
Mine should be true for this: s = "1293" and false for this s= "255xxxxyyy.
Yours will return true for anything except this s="ABC" (anything without a
number).
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
?Thanks for reply. Which one should return false, the one you send
(Regex.IsMatch(s, "^\d+$") or mine (Regex.IsMatch(s, "[0-9]")? Mine
returns
true in VB.net 2003.

"Dr Screwup" wrote:
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
> Should not the following return False if s="255xxxxyyy"? It seems to
> return
> True.
>
> Regex.IsMatch(s, "[0-9]")
> --
> Dennis in Houston


Nov 21 '05 #4

P: n/a
Thanks. I guess I really had it wrong. I got the Regex string from one of
the previous notes on this newsgroup.

"Dr Screwup" wrote:
Mine should be true for this: s = "1293" and false for this s= "255xxxxyyy.
Yours will return true for anything except this s="ABC" (anything without a
number).
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
?Thanks for reply. Which one should return false, the one you send
(Regex.IsMatch(s, "^\d+$") or mine (Regex.IsMatch(s, "[0-9]")? Mine
returns
true in VB.net 2003.

"Dr Screwup" wrote:
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
> Should not the following return False if s="255xxxxyyy"? It seems to
> return
> True.
>
> Regex.IsMatch(s, "[0-9]")
> --
> Dennis in Houston


Nov 21 '05 #5

P: n/a
Dennis,
Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]") If you try it, you will find it returns True, as you are looking for a
single digit 0 thru 9 anyplace in the input string. The string "255xxxxyyy"
contains 3 matches, the first 2, the first 5 & the second 5.

While: Regex.IsMatch(s, "^\d+$") Will return false for "255xxxxyyy", as you are looking for the beginning of
the string "^", followed by one or more digits "\d+", followed by the end of
the string "$".

For example, try the following code for both patterns:

Imports System.Text.RegularExpressions

Const theFirstPattern As String = "[0-9]"
Const theSecondPattern As String = "^\d+$"
Const input As String = "255xxxxyyy"

Dim theFirstRegex As New Regex(theFirstPattern)
Debug.WriteLine(theFirstRegex.IsMatch(input), "first is match")
For Each aMatch As Match In theFirstRegex.Matches(input)
Debug.WriteLine(aMatch.Value, aMatch.Index.ToString())
Next

Dim theSecondRegex As New Regex(theSecondPattern)
Debug.WriteLine(theSecondRegex.IsMatch(input), "second is match")
For Each aMatch As Match In theSecondRegex.Matches(input)
Debug.WriteLine(aMatch.Value, aMatch.Index.ToString())
Next

The following site provides a good overview of regular expressions:

http://www.regular-expressions.info/

While this site provides the syntax specifically supported by .NET:

http://msdn.microsoft.com/library/de...geElements.asp

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com... Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]")
--
Dennis in Houston

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.