简体   繁体   English

从文本中提取带指数的双数值

[英]Extract double number value with exponent from text

How could I extract a number of type double which may have exponent from a string with more character? 如何从具有更多字符的字符串中提取可能具有指数的double类型的数字?

For example extract 56.8671311035e-06 from 例如从中提取56.8671311035e-06

"this is a string with a number inside 56.8671311035e-06 and the string continues here" “这是一个字符串,其内部数字为56.8671311035e-06,该字符串在此处继续”

I guess it could be done using regular expressions, but my knowledge of them is very limited. 我猜可以使用正则表达式来完成,但是我对它们的了解非常有限。

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]+)?"

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM