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
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
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+)?$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With