Wednesday, February 4, 2009

Regular Expression for Decimal Scientific and Fraction Values : ALL IN ONE

Regular expressions are really handy when user input needs to be validated.The following expression can be used to validate Decimal or Fraction or Scientific values.

^(([+-]?([0-9]*\d*\.\d{1}?\d*))(\-?(?:((\d+\s*)?\d+\/[1-9]+[0-9]*)(\d+(\s*\d+\/[1-9]+[0-9]*)?)))(^[+-]?[0-9]*(?:\.\d?)?[Ee][-+]?\d+))$

The big list of matches and non-matches below is the hard work of one of my colleagues.

Matches
0 ,0.0 ,-0 ,-0.0 ,+0 ,+0.0 ,0.1 ,-0.1 ,+0.1 ,1 ,1.1 ,-1 ,-1.1 ,+1 ,+1.1 ,1.1234567890 ,-1.1234567890 ,1.12345678901234567890 ,-1.12345678901234567890 ,0E0 ,-0E0 ,+0E0 ,
1E2 ,1E+2 ,1E-2 ,-1E2 ,-1E+2 ,-1E-2 ,+1E2 ,+1E+2 ,+1E-2 ,1.E+2 ,1.E-2 ,1.0E+2 ,1.0E-2 ,
.0E+2 ,-.0E+2 ,+.0E+2 ,.1E+2 ,-.1E+2 ,+.1E+2 ,12345678901234567890E+2 ,1.0e+2 ,0/1 ,
-0/1 ,1/1 ,-1/1 ,1 0/1 ,1 1/1 ,-1 1/1 ,0 1/1 ,-0 1/1 ,1/8 ,-1/8 ,1 1/8 ,-1 1/8 ,10/8 ,12345/12346

Non-Matches
. ,&5 ,1,2 ,1d5 ,-1- ,-1.-1 ,+1+ ,+1.1+ ,E ,.E ,E0 ,0E ,-E0 ,-0E ,+E0 ,+0E ,1E++2 ,1E+-2 ,1E+2- ,
1E+2+ ,1E-+2 ,1E--2 ,1E-2- ,1E-2+ ,1E+.2 ,1E+2. ,1E+2.0 ,1.E+.2 ,1.E+2. ,.E+2 ,-.E+2 ,+.E+2 ,
.-E+2 ,.+E+2 ,-.E+2 ,1+E+2 ,1-E+2 ,1.+E+2 ,1.0+E+2 ,0/0 ,1/0 ,1 0/0 ,1 1/0 ,0 0/0 ,1-/8 ,1/-8 ,
1/+8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