-2

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
David
  • 15,894
  • 22
  • 55
  • 66
user2301717
  • 1,049
  • 1
  • 10
  • 13
  • Please refer http://stackoverflow.com/questions/2072222/regular-expression-for-positive-and-a-negative-decimal-value-in-java – Mayank Jain May 18 '13 at 08:03
  • http://stackoverflow.com/questions/308122/simple-regular-expression-for-a-decimal-with-a-precision-of-2 - maybe it will help you? – Fisk May 18 '13 at 08:05

2 Answers2

3

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+))
MofX
  • 1,569
  • 12
  • 22
0

I would try something like:

^[+-]?(\d+(\.?\d+)?|\.\d+)$

Matches all your examples, and numbers like:

  • +1.0
  • .56
  • +.56
  • -.56
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103