Is it possible to extract positive integers, negative integers, positive decimal number and negative decimal number using Regex?
I have been using -\d+
regular expression to get both positive and negative numbers.
Valid Numbers:
-1
-1.0
1
1.0
Is it possible to extract positive integers, negative integers, positive decimal number and negative decimal number using Regex?
I have been using -\d+
regular expression to get both positive and negative numbers.
Valid Numbers:
-1
-1.0
1
1.0
The regex
[+-]?\d+(?:\.\d+)?
would accept numbers like
1123
1.00
+123213
-123.234324
if you also like to match numbers like .23 you have can use
[+-]?(?:(?:\d+)|(?:\d*\.\d+))
I would try something like:
^[+-]?(\d+(\.?\d+)?|\.\d+)$
Matches all your examples, and numbers like:
+1.0
.56
+.56
-.56