Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 1st, 2009, 06:12 PM
Wimnat Wimnat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Abertawe, Cymru
Posts: 29 Wimnat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 35 sec
Reputation 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,

Reply With Quote
  #2  
Old April 1st, 2009, 07:29 PM
latros latros is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 233 latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 16 h 38 m 21 sec
Reputation Power: 75
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

Reply With Quote
  #3  
Old April 1st, 2009, 09:12 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,711 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 6 h 48 m 5 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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*$/

Reply With Quote
  #4  
Old April 2nd, 2009, 05:55 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 37 m 21 sec
Reputation Power: 3885
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.

Reply With Quote
  #5  
Old April 2nd, 2009, 07:31 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
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.9999999999999999999999999999 99999999999"? Many programming languages will not handle these large numbers (at least not as precise) by default.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Is_numeric

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap