Regex Number Check

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Should not the following return False if s="255xxxxyyy"? It seems to return
True.

Regex.IsMatch(s, "[0-9]")
 
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")
 
?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 said:
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")


Dennis said:
Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]")
 
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 said:
?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 said:
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")


Dennis said:
Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]")
 
Thanks. I guess I really had it wrong. I got the Regex string from one of
the previous notes on this newsgroup.

Dr Screwup said:
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 said:
?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 said:
Yes, it should return false.

'-- Check for number
Regex.IsMatch(s, "^\d+$")


Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

Regex.IsMatch(s, "[0-9]")
 
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/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay



Dennis said:
Should not the following return False if s="255xxxxyyy"? It seems to
return
True.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

regex validate rangename 8
Newbie question about Regex 8
Common Regex.IsMatch expressions 3
Regular Expressions problem 2
RegexOptions.Multiline? 1
Regex references 4
regular expression pattern ? 2
The regex 3

Back
Top