Thread: Is_numeric

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Abertawe, Cymru
    Posts
    29
    Rep Power
    0

    Is_numeric


    Hey,

    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?

    Thanks,
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    233
    Rep Power
    76
    I am no expert at regex, but I would guess like this

    Air coded ~ (VB.NET)
    Code:
    Imports System.Text
    
    Public Class Form1
        Private Function DoesItMatch (ByVal pInput As String, ByVal pEvaluator As String) As Boolean
            If RegularExpressions.Regex.IsMatch(pInput, pEvaluator) Then
                Return True
            Else
                Return False
            End If
        End Function
    
        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")
            Else
                MessageBox.Show("It is NOT 0 through 9")
            End If
    
    
            '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")
            Else
                MessageBox.Show("It is NOT numeric")
            End If
        End Sub
    End Class
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    If you want something like PHP's is_numeric (which checks for a number, not necessarily an integer) then this should be quite enough.
    Code:
    /^\s*[+-]?\d*(\.\d*)?([eE][+-]?\d+)?\s*$/
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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.
  8. #5
  9. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by ishnid
    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.
    It depends on what language the OP is using. In many languages the "0." is a valid floating point literal.

    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.

IMN logo majestic logo threadwatch logo seochat tools logo