0

我正在尝试将 Regex 用于我正在做的工作项目。我有一组看起来像这样的数字:

23    14    62  -121    98   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
24    13    64  -118   101   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
19    10    65  -124    93   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
19    11    62  -130    93   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
16     2    65  -127    83   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
18     1    68  -127    86   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
29    -1    64  -129    92   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
22     2    63  -131    87   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
16    13    62  -130    91   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
15     6    66  -131    87   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
16     2    62  -137    80   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
16    -5    63  -133    74   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
24    -1    60  -135    83   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
15    11    59  -137    86   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
11     8    64  -131    83   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
19    10    64  -130    92   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
20    11    65  -136    96   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
20     8    59  -136    87   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
18    13    59  -135    90   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
18    10    60  -138    88   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
23     6    60  -133    88   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
20    10    57  -127    87   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
23     4    61  -127    88   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
14    -3    63  -131    75   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
16    -5    62  -129    73   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
25    -6    62  -127    80   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
21     2    60  -129    83   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
14     3    65  -133    81   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
14     8    64  -132    86   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
19    11    59  -131    89   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
28     5    59  -129    93   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
29    -3    56  -130    82   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
24     0    58  -128    82   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
28    12    65  -128   104   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
25     4    65  -123    94   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
17    -1    61  -126    77   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...
18     2    62  -130    82   -0.0   -0.1    0.0   -0.2 165 60.00 .... ...

我想只用一个或两个正则表达式语句来获得每行中的第 10 个数字(165)。该数字偶尔会从 165 更改,因此我无法对其进行硬编码。到目前为止,我有:

([+-]{0,1}[0-9]{1,5})
([+-]{0,1}[0-9]{1,5})
([+-]{0,1}[0-9]{1,5})
([+-]{0,1}[0-9]{1,5})
([+-]{0,1}[0-9]{1,5})
([+-]{0,1}[0-9]{1,}[.]{0,1}[0-9]{0,5})|([+-]{0,1}[.]{1,1}(?=[0-9])[0-9]{0,5}))
([+-]{0,1}[0-9]{1,}[.]{0,1}[0-9]{0,5})|([+-]{0,1}[.]{1,1}(?=[0-9])[0-9]{0,5}))
([+-]{0,1}[0-9]{1,}[.]{0,1}[0-9]{0,5})|([+-]{0,1}[.]{1,1}(?=[0-9])[0-9]{0,5}))
([+-]{0,1}[0-9]{1,}[.]{0,1}[0-9]{0,5})|([+-]{0,1}[.]{1,1}(?=[0-9])[0-9]{0,5}))
([+-]{0,1}[0-9]{1,5})

这显然不是 1 或 2 步,而是 10 加,它给了我 9 分,我不想要。

这个问题已经解决了,但是现在又出现了一个新问题:

我以为:

(?#<INS:5>)
^.{53}([+-]?\d+)
\.\.\.\. \.\.\.
(?#<INS:5>)
^.{53}([+-]?\d+)
\.\.\.\. \.\.\.
(?#<INS:5>)
^.{53}([+-]?\d+)
\.\.\.\. \.\.\.

解决了我的问题,但事实证明此代码在以下情况下会中断:

  9486     9    68  -133  9562   -0.0   -0.1    0.0   -0.2 106 60.00 .... ...
  9455     3    63  -129  9521   -0.0   -0.1    0.0   -0.2 106 60.00 .... ...
  9417     3    64  -132  9485   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  9367     3    60  -129  9431   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  9305    12    56  -131  9373   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  9237    12    66  -135  9315   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  9170     2    65  -129  9238   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  9111     4    62  -127  9177   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  9041    -0    58  -126  9099   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  8969     6    57  -129  9032   -0.0   -0.1    0.0   -0.2  89 60.00 .... ...
  8887     9    60  -132  8956   -0.0   -0.1    0.0   -0.2  73 60.00 .... ...
  8802     5    62  -131  8869   -0.0   -0.1    0.0   -0.2  73 60.00 .... ...
  8720     1    64  -132  8785   -0.0   -0.1    0.0   -0.2  73 60.00 .... ...
  8634     9    66  -137  8710   -0.0   -0.1    0.0   -0.2  73 60.00 .... ...

当第 10 个数字低于 100 时,代码失败。有没有办法让它在 10 秒和 1 秒内不会中断?

4

2 回答 2

1

您可以尝试添加[ ]*到正则表达式。这应该抓取 106 或 89。它所做的只是抓取一个额外的空间,如果它存在于数字之前。

(?#<INS:5>)
^.{53}[ ]*([+-]?\d+)
\.\.\.\. \.\.\.
(?#<INS:5>)
^.{53}[ ]*([+-]?\d+)
\.\.\.\. \.\.\.
(?#<INS:5>)
^.{53}[ ]*([+-]?\d+)
\.\.\.\. \.\.\.
于 2013-09-11T06:31:08.467 回答
0

这对我有用,仅以一个字符串为例。

   var someString="9486     9    68  -133  9562   -0.0   -0.1    0.0   -0.2 106 60.00 0909";
   var getum =someString.match(/[\-\+]?[0-9]*\.?[0-9]+/g);
   getnum[9] // gives 106

注意:使用 javascript

于 2013-09-11T06:14:09.270 回答