SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: RegEx Help

  1. #1
    SitePoint Addict inverse.chi's Avatar
    Join Date
    May 2006
    Location
    Oxford, UK | Durham, UK
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegEx Help

    Hi,

    I am trying to make a regular expression that will match any whole integer or any decimal number for example

    182, 123.38378, 2.99

    i would have thought it was something like

    ^(\d+\.\d+)|\d+$

    but i dont think this will work.

    any help - links - guidance would be useful

    ---- i solves this because my backslash for the . was wrong

    ^(\d+/.\d+)|\d+$
    Last edited by inverse.chi; Aug 8, 2008 at 06:20. Reason: problem solved
    Malachi Soord
    Something will come here

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by inverse.chi View Post
    Hi,

    I am trying to make a regular expression that will match any whole integer or any decimal number for example

    182, 123.38378, 2.99
    Code:
    /^[\-\+]?(\d+\.|\.\d+|\d+\.\d+|\d+)$/
    This should be preceded by a routine that removes all spaces from the string.
    Tab-indentation is a crime against humanity.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The above regex will also match 123. which isn't valid.

    www.regexlib.com is a very good source of regular expressions.
    For example, they have the following

    /^\d*[0-9](\.\d*[0-9]|,\d*[0-9])?$/

    which if you don't care about international number formats, could be simplified to

    ^\d*[0-9](\.\d*[0-9])?$

    edit: |. has been corrected to be \.
    Last edited by paul_wilkins; Aug 12, 2008 at 17:01.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    The above regex will also match 123. which isn't valid.
    The validity of a number depends upon the system that interprets it, and I see nothing wrong with ending a number with a decimal separator, if only to clarify its position. parseFloat doesn't consider it invalid.
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    ^\d*[0-9](|.\d*[0-9])?$
    That will match something like 5@1. You need to escape the period.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Young Twig View Post
    That will match something like 5@1. You need to escape the period.
    Well spotted, the tyop has been fixed.

    ^\d*[0-9](\.\d*[0-9])?$
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Addict inverse.chi's Avatar
    Join Date
    May 2006
    Location
    Oxford, UK | Durham, UK
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers for the advice!
    Malachi Soord
    Something will come here


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •