简体   繁体   English

这种科学符号在C#中的正则表达式将如何?

[英]How would be this scientific notation regular expression in C#?

I'm using this regular expression for a scientific notation and setting to REGEX class in C#. 我将此正则表达式用于科学记数法,并设置为C#中的REGEX类。

[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?

But it's not really working. 但这不是真的。

An alternative that's likely easier: 一个可能更容易的替代方法:

double.TryParse(stringInput, out doubleOutput);

Double.TryParse handles scientific notation by default. Double.TryParse默认情况下处理科学计数法。

What's not working about it? 什么不起作用呢?

One potential problem I see is that you're allowing leading 0s, and also trailing 0s in the decimal. 我看到的一个潜在问题是,您允许前导0,并且在小数后还允许尾随0。 Not sure if you want either, both, or neither. 不确定是否要两者都选。 You should also make the decimal portion optional, but dependent on the existence of the decimal point. 您还应该使小数部分为可选,但要取决于小数点的存在。 Here's what I'd recommend: 这是我的建议:

[-+]?(0?|[1-9][0-9]*)(\.[0-9]*[1-9])?([eE][-+]?(0|[1-9][0-9]*))?

That will match: 这将匹配:

0
0.1
0.01
1
1.1
1.01
10
10.1
10.01
1e1
1
1e0
1E1
1e10

It won't match: 不匹配:

.
01
1.0
1.10
1e01

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

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