我希望能够写,其接收数字科学记数法为字符串和打出它的系数和指数作为单独项目的功能。我可以只使用正则表达式,但来电号码可能不是标准化的,我宁愿要能够正常化,然后打破部分出来。

一个同事已经得到了一个解决方案的一部分使用方式VB6但它不是很那里,如下面示出的转录物。

cliVe> a = 1e6
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 10 exponent: 5 

本来应该1和6

cliVe> a = 1.1e6
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.1 exponent: 6

正确

cliVe> a = 123345.6e-7
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.233456 exponent: -2

正确

cliVe> a = -123345.6e-7
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.233456 exponent: -2

应-1.233456和-2

cliVe> a = -123345.6e+7
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.233456 exponent: 12

正确

任何想法?顺便说一句,克里夫是基于一个的VBScript CLI和上可以找到我的博客

有帮助吗?

解决方案

谷歌上 “科学记数法的regexp” 示出了许多的匹配,包括这一个(的不使用它!!!! ),其使用

*** warning: questionable ***
/[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/

,它包括例如-.5e7和+ 00000e33(两者可能不希望允许)。

相反,我会建议您使用语法上的道格·克罗克福德的 JSON网站,其中明确记载什么构成JSON的数字。下面是从该网页采取了相应的语法图:

“ALT文本” 结果 <子>(来源: json.org

如果你看看他 json2.js 脚本(安全转换从JSON在JavaScript /),你会看到一个正则表达式的这一部分:

/-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/

其中,具有讽刺意味的,不符合他的语法图....(貌似我应该提交一个bug)我认为,确实实现这种语法图中的正则表达式是这样的一个:

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

如果你想允许初始+还有,您可以:

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

添加捕获括号根据自己的喜好。

我也强烈建议你充实一堆测试用例,以确保您包括要包括(或不包括)这些可能性,如:

allowed:
+3
3.2e23
-4.70e+9
-.2E-4
-7.6603

not allowed:
+0003   (leading zeros)
37.e88  (dot before the e)

祝你好运!

其他提示

Building off of the highest rated answer, I modified the regex slightly to be /^[+\-]?(?=.)(?:0|[1-9]\d*)?(?:\.\d*)?(?:\d[eE][+\-]?\d+)?$/.

The benefits this provides are:

  1. allows matching numbers like .9 (I made the (?:0|[1-9]\d*) optional with ?)
  2. prevents matching just the operator at the beginning and prevents matching zero-length strings (uses lookahead, (?=.))
  3. prevents matching e9 because it requires the \d before the scientific notation

My goal in this is to use it for capturing significant figures and doing significant math. So I'm also going to slice it up with capturing groups like so: /^[+\-]?(?=.)(0|[1-9]\d*)?(\.\d*)?(?:(\d)[eE][+\-]?\d+)?$/.

An explanation of how to get significant figures from this:

  1. The entire capture is the number you can hand to parseFloat()
  2. Matches 1-3 will show up as undefined or strings, so combining them (replace undefined's with '') should give the original number from which significant figures can be extracted.

This regex also prevents matching left-padded zeros, which JavaScript sometimes accepts but which I have seen cause issues and which adds nothing to significant figures, so I see preventing left-padded zeros as a benefit (especially in forms). However, I'm sure the regex could be modified to gobble up left-padded zeros.

Another problem I see with this regex is it won't match 90.e9 or other such numbers. However, I find this or similar matches highly unlikely as it is the convention in scientific notation to avoid such numbers. Though you can enter it in JavaScript, you can just as easily enter 9.0e10 and achieve the same significant figures.

UPDATE

In my testing, I also caught the error that it could match '.'. So the look-ahead should be modified to (?=\.\d|\d) which leads to the final regex:

/^[+\-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:\d[eE][+\-]?\d+)?$/

Here is some Perl code I just hacked together quickly.

my($sign,$coeffl,$coeffr,$exp) = $str =~ /^\s*([-+])?(\d+)(\.\d*)?e([-+]?\d+)\s*$/;

my $shift = length $coeffl;
$shift = 0 if $shift == 1;

my $coeff =
  substr( $coeffl, 0, 1 );

if( $shift || $coeffr ){
  $coeff .=
    '.'.
    substr( $coeffl, 1 );
}

$coeff .= substr( $coeffr, 1 ) if $coeffr;

$coeff = $sign . $coeff if $sign;

$exp += $shift;

say "coeff: $coeff exponent: $exp";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top