Sunday, 15 May 2011

c# - What's the shortest regex that can match non-zero floating point numbers with any number of decimal places? -


Can Minimum Regex match any non-zero floating point numbers with any number of decimal?

It should accept numbers like

  -1-5.9652 -7.00002 -0.8 -0.0500 -0.58000 0.01 0.000005 0.9 900 5 7.5 7.005  

but reject the creation such as

  .02 -. -990 0-0.0 0 -0.000 0.00 - .. +0 + 1 + +1.26, etc.  

Support me for scientific notation with E, E and such is not needed.
The language I am using is C #, the way it is.

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

Edit Updated to reflect the new requirements (last decimal may be zero)

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

(LookHead's Less than used: ^ - (? (? ([= 1- 9]) \ d * | [1- 9] \ d * (\. \ D +)?) $ .)


EDIT2 if the example can pass 001.000

  ^ -? (? =? . * [1- 9]) \ d + (\. \ D +)? $  

No comments:

Post a Comment