2

我正在尝试将 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 分,我不想要。有没有办法在只有 1 或 2 条语句中得到这个?

4

5 回答 5

1

编辑:如果您不必使用正则表达式,但可以使用 Awk 例如,您可以使它更简单:

cat file.dat | awk '{print $10}'

这是我的变化:

^(?:[\S-\.]+\s+){9}(\S+)

然后,您的号码(即 165)将存储在第一个捕获组中(即$1,取决于您使用的语言)。

解释:

^             // Beginning of the line
(?:           // Do not capture this group (we're not interested in it)
  \S+         //   Any non-space character - one or more times
  \s+         //   Followed by a white-space character (one or more)
){9}          // Repeat the above nine times
(\S+)         // Any non-white space characters (our tenth number)

Perl 中的使用示例:

cat file.dat | perl -ne 'print $1 if s/(?:\S+\s+){9}(\S+)//'
于 2013-09-05T20:39:21.580 回答
1

如果所有行中的所有列都是固定宽度,您可以这样做:

^.{55}(\d+)

请注意,这旨在匹配诸如“165”之类的数字,没有符号或小数部分。这可能更灵活:

^.{55}([+-]?\d+(?:.\d+)?)
于 2013-09-05T20:39:21.763 回答
0

您没有指定您使用的语言/工具,所以这里有一个 Java 示例:

// Split the input into each row:
final String rows[ ] = INPUT.split( "\\n" );

// Iterate through each row:
for ( final String row : rows )
{
  // Split the row into components separated by spaces:
  final String components[ ] = row.split( "\\s+" );

  assert components.length < 9 : "There is no 10th number!";

  final double number = Double.parseDouble( components[ 9 ] );
}
于 2013-09-05T20:40:09.907 回答
0
^\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+)

或者,甚至更好(感谢@SamIAm!):

^(\S+\s+){9}(\S+)
于 2013-09-05T20:36:17.260 回答
0

我想出了这个正则表达式

/^.*?(?:[\d\.]+[^\d\.]+?){9}([\d\.-]+)/

http://rubular.com/r/0cwhaL92aw

于 2013-09-05T20:36:25.427 回答