April 1st, 2009, 06:12 PM
I would like the equivilant of an is_numeric() function in regex. THe number can be any length and any digit 0-9.
How can I do this?
April 1st, 2009, 07:29 PM
I am no expert at regex, but I would guess like this
Air coded ~ (VB.NET)
Public Class Form1
Private Function DoesItMatch (ByVal pInput As String, ByVal pEvaluator As String) As Boolean
If RegularExpressions.Regex.IsMatch(pInput, pEvaluator) Then
Private Sub MySub()
'Get the string that we want to evaluate
Dim strInput As String = TextBox1.Text
'Is it a number 0 through 9?
Dim boolIsZeroThroughNine As Boolean = DoesItMatch(strInput, "\b[0-9]\b") 'Note: In VB.NET the beginning & end of a string are denoted with a "\b", where in most other RegEx expressions, it is "^" & "$" respectively.
If boolIsZeroThroughNine = True Then
MessageBox.Show("It is 0 through 9")
MessageBox.Show("It is NOT 0 through 9")
'Or, just check if it is numeric
Dim boolIsNumeric As Boolean = DoesItMatch(strInput, "\b\d\b") 'I believe the "\d" stands for decimal
If boolIsNumeric = True Then
MessageBox.Show("It is numeric")
MessageBox.Show("It is NOT numeric")
April 1st, 2009, 09:12 PM
If you want something like PHP's is_numeric (which checks for a number, not necessarily an integer) then this should be quite enough.
April 2nd, 2009, 05:55 AM
Would you not want a \d+ after the decimal point. I wouldn't think that you'd want to have a decimal point with nothing after it.
April 2nd, 2009, 07:31 AM
It depends on what language the OP is using. In many languages the "0." is a valid floating point literal.
Originally Posted by ishnid
But that's not all the OP has neglected to mention. Perhaps the OP is validating some string to see if it's a valid number and after making sure it is, will be parsing the number. What will happen if the number is the following string: "10000000000000000000000000000000000000000000000000000000000000000000000.999999999999999999999999999 999999999999"? Many programming languages will not handle these large numbers (at least not as precise) by default.