Skip to content Skip to sidebar Skip to footer

Regular Expression For Non-zero Positive Floats

Regex for positive float numbers provides almost the correct expressions, but all of them include 0.0 and 0. Closest desired expression is: ^(?:[1-9]\d*|0)?(?:\.\d+)?$ but this i

Solution 1:

Use

^(?:[1-9]\d*|0(?!(?:\.0+)?$))?(?:\.\d+)?$

See proof.

Explanation

--------------------------------------------------------------------------------^                        the beginning of the string
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [1-9]                    anycharacterof: '1'to'9'--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------|OR--------------------------------------------------------------------------------0'0'--------------------------------------------------------------------------------
    (?!                      look ahead to see if there isnot:
--------------------------------------------------------------------------------
      (?:                      group, but do not capture (optional
                               (matching the most amount possible)):
--------------------------------------------------------------------------------
        \.                       '.'--------------------------------------------------------------------------------0+'0' (1or more times (matching the
                                 most amount possible))
--------------------------------------------------------------------------------
      )?                       endofgrouping--------------------------------------------------------------------------------
      $                        before an optional \n, and the endof
                               the string
--------------------------------------------------------------------------------
    )                        endof look-ahead
--------------------------------------------------------------------------------
  )?                       endofgrouping--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )?                       endofgrouping--------------------------------------------------------------------------------
  $                        before an optional \n, and the endof the
                           string

Solution 2:

The accepted answer from the linked question can be adapted by removing the choice of 0 from the first group, and choosing between that group and another group that starts with 0.

^(((?:[1-9]\d*)?(?:\.\d+)?)|(0\.[1-9]\d*))$

This works too:

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

Basically, this is "saying" that if the number starts with 0, it has to be followed by a dot and something other than 0. After that, any digit is permissible. So, 0 and 0.0 aren't permitted, but 0.1 and 0.10 are.

Note: This Regex will not permit leading 0s. So, for example 101.0 is permissible but 0101.0 is not.

Edit: The following will allow for cases like 0.01:

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

Post a Comment for "Regular Expression For Non-zero Positive Floats"