-1

How could I extract a number of type double which may have exponent from a string with more character?

For example extract 56.8671311035e-06 from

"this is a string with a number inside 56.8671311035e-06 and the string continues here"

I guess it could be done using regular expressions, but my knowledge of them is very limited.

anderZubi
  • 6,414
  • 5
  • 37
  • 67
  • **Possible duplicate:** [Parsing scientific notation sensibly?](http://stackoverflow.com/q/638565/1497596) – DavidRR May 03 '14 at 23:10

2 Answers2

7

Yep, I'd say regular expressions are your friend here:

var match = Regex.Match(input, @"[0-9.]+e[-+][0-9]+");

Or you can prevent matching multiple decimal points with the following (the last one will be treated as the "proper" one):

@"\b[0-9]+(.[0-9]+)e[-+][0-9]+\b"

Edit: Here's a more complete one that will allow optional exponents and will also allow for the decimal point to be at the start of the number:

@"[\d]*\.?[\d]+(e[-+][\d]+)?"
Ant P
  • 24,820
  • 5
  • 68
  • 105
3

You can do this:

string test = "this is a string with a number inside 56.8671311035e-06 and the string continues here";
string expoNum = Regex.Match(test,@"[\d.]+e[-+]?\d+").Value;
Amit Joki
  • 58,320
  • 7
  • 77
  • 95