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

C# 从文本中提取带指数的双数值,c#,regex,string-parsing,C#,Regex,String Parsing,如何从字符数较多的字符串中提取可能具有指数的double类型数 例如,从中提取56.8671311035e-06 “这是一个数字在56.8671311035e-06内的字符串,该字符串在此继续” 我想可以使用正则表达式来实现,但我对它们的了解非常有限。您可以这样做: string test = "this is a string with a number inside 56.8671311035e-06 and the string continues here"; string expoNu

如何从字符数较多的字符串中提取可能具有指数的double类型数

例如,从中提取
56.8671311035e-06

“这是一个数字在56.8671311035e-06内的字符串,该字符串在此继续”

我想可以使用正则表达式来实现,但我对它们的了解非常有限。

您可以这样做:

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;

是的,我想说正则表达式是你的朋友:

var match = Regex.Match(input, @"[0-9.]+e[-+][0-9]+");
或者,您可以防止将多个小数点与以下内容匹配(最后一个小数点将被视为“正确的”小数点):

编辑:这里有一个更完整的选项,它允许可选的指数,并且允许小数点位于数字的开头:

@"[\d]*\.?[\d]+(e[-+][\d]+)?"

e-
应该是
e[-+]?
事实上,
e[-+][0-9]+
应该是
(e[-+][0-9]+)?
正如Q所说的“可能有指数”。
e-
应该是
e[-+]?
以匹配56.8671311035e-06或56.8671311035e+06或56。8671311035e06@ClickRick,谢谢。我不知道如何定义它们,
e[-+][0-9]+
应该是
(e[-+][0-9]+?
正如问题所说的“可能有指数”。@ClickRick,谁是Q?可能重复:
@"[\d]*\.?[\d]+(e[-+][\d]+)?"