Monday, January 10, 2011

A Regular Expression for Multiple Forms of Scientific Notation

I needed to create a regular expression to match numbers formatted in scientific notation.  It turns out there are many forms used around the world, so coming up with a regex to match all possible variations was a bit of a challenge.  Well, here it goes... 

I found the following “acceptable” forms of scientific notation:
123,000,000,000 can be written as:
1.23E+11
1.23e+11
1.23X10^11
1.23x10^11
1.23X10*11
1.23x10*11

0.000001 can be written as:
1.0E-6
1.0e-6
1.0X10^-6
1.0x10^-6
1.0X10*-6
1.0x10*-6

Additionally…
  • A positive or a negative sign can be appended to the beginning to represent a positive or a negative number respectively. For example:
    • +1.0E-6
    • -1.0E-6
  • The decimal point and the standalone 0 following the decimal point may be omitted. For example:
    • 1E-6
  • The positive sign following the E, e, ^, or * may be omitted. For example:
    • 1.23E11

The regular expression I derived to match all forms mentioned above is:
^[+|-]?\d\.?\d{0,}[E|e|X|x](10)?[\^\*]?[+|-]?\d+$
 

No comments:

Post a Comment