Reputation: 53
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 includes 0.0
and 0
.
What is the regex expression for positive floating numbers such as:
1.0
1.1
0.1
11
but do not include floating numbers such as:
0.0
-0.0
0
-1.0
-1.1
-0.1
-11
Upvotes: 4
Views: 1616
Reputation: 828
This one is the best:
^\.0*[1-9]\d*$|^0(\.0*[1-9]\d*)$|^[1-9]\d*?(\.\d+)?$
See how it works on regex101.
If you don't want to accept floats like .5
, use this one:
^0(\.0*[1-9]\d*)$|^[1-9]\d*?(\.\d+)?$
Upvotes: 1
Reputation: 12191
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*))$
Upvotes: 0
Reputation: 18641
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] any character of: '1' to '9'
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
0 '0'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
0+ '0' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Upvotes: 2