Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用perl分割空白_Regex_Perl - Fatal编程技术网

Regex 使用perl分割空白

Regex 使用perl分割空白,regex,perl,Regex,Perl,我有一行代码来分割文本文件中的空白: my @line_splits = split /\s+/, $ine; 1 2016-09-27 14:07:20.084877 2 2016-09-27 14:07:20.084998 3 2016-09-27 14:07:20.131343 4 2016-09-27 14:07:20.131374 6 2016-09-27 14:07:20.137359 7 2016-09-27 14:07:20.137556 8 201

我有一行代码来分割文本文件中的空白:

my @line_splits = split /\s+/, $ine;
  1 2016-09-27 14:07:20.084877
  2 2016-09-27 14:07:20.084998
  3 2016-09-27 14:07:20.131343
  4 2016-09-27 14:07:20.131374
  6 2016-09-27 14:07:20.137359
  7 2016-09-27 14:07:20.137556
  8 2016-09-27 14:07:20.137796
  9 2016-09-27 14:07:20.437769
 10 2016-09-27 14:07:20.437796
100 2016-09-27 14:07:23.293661 
这是文本文件的几个内容:

my @line_splits = split /\s+/, $ine;
  1 2016-09-27 14:07:20.084877
  2 2016-09-27 14:07:20.084998
  3 2016-09-27 14:07:20.131343
  4 2016-09-27 14:07:20.131374
  6 2016-09-27 14:07:20.137359
  7 2016-09-27 14:07:20.137556
  8 2016-09-27 14:07:20.137796
  9 2016-09-27 14:07:20.437769
 10 2016-09-27 14:07:20.437796
100 2016-09-27 14:07:23.293661 
我的目标是获取日期和时间(我知道是在$line_splits[1]和$line_splits[2]上)

但是当我运行perl脚本时,从1到99的行是错误的,而在第100行及以上,我得到了我想要的

Time Stamp: 98 2016-09-27               --> line 98
Time Stamp: 99 2016-09-27               --> line 99
Time Stamp: 2016-09-27 14:07:23.293661  --> line 100
Time Stamp: 2016-09-27 14:07:23.299406  --> line 101
Time Stamp: 2016-09-27 14:07:23.299437  --> line 102
有人能告诉我正则表达式有什么问题吗?或者有其他方法可以做到这一点吗

我不知道它是否已经在这里复制,但任何帮助将非常感谢


谢谢:)

在按空格拆分之前,请从每行的左侧修剪空白:

$line =~ s/^\s+//;
my @line_splits = split /\s+/, $line;
像往常一样,@Wiktor加入了这个选项,它将保留数字前面的空格小于100:

my @line_splits = split /^\s+(*SKIP)(*F)|\s+/, $line;
在Perl 6中,将有一个真正的
trim
函数:

$line .= trim;
my @line_splits = split /\s+/, $line;

另一种选择是使用正则表达式提取您感兴趣的字符串位(即所有非空白位)

#/usr/bin/perl
严格使用;
使用警告;
使用5.010;
而(){
我的@line_拆分=/(\S+)/g;
说“时间戳:$line_splits[1]$line_splits[2]”;
}
__资料__
1 2016-09-27 14:07:20.084877
2 2016-09-27 14:07:20.084998
3 2016-09-27 14:07:20.131343
4 2016-09-27 14:07:20.131374
6 2016-09-27 14:07:20.137359
7 2016-09-27 14:07:20.137556
8 2016-09-27 14:07:20.137796
9 2016-09-27 14:07:20.437769
10 2016-09-27 14:07:20.437796
100 2016-09-27 14:07:23.293661

哇,有很多复杂的答案,但解决方法真的很简单

只需使用
split
而不使用
/\s+/
regex

如果只指定要拆分的
”,则会忽略前导空格。但是对于
/\s+/
,它不会

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

while ( <DATA> ) {
  chomp;
  my @fields  = split;
  print $fields[2],"\n"
}

__DATA__
  1 2016-09-27 14:07:20.084877
  2 2016-09-27 14:07:20.084998
  3 2016-09-27 14:07:20.131343
  4 2016-09-27 14:07:20.131374
  6 2016-09-27 14:07:20.137359
  7 2016-09-27 14:07:20.137556
  8 2016-09-27 14:07:20.137796
  9 2016-09-27 14:07:20.437769
 10 2016-09-27 14:07:20.437796
100 2016-09-27 14:07:23.293661 
#/usr/bin/env perl
严格使用;
使用警告;
使用数据::转储程序;
而(){
咀嚼;
我的@fields=split;
打印$fields[2],“\n”
}
__资料__
1 2016-09-27 14:07:20.084877
2 2016-09-27 14:07:20.084998
3 2016-09-27 14:07:20.131343
4 2016-09-27 14:07:20.131374
6 2016-09-27 14:07:20.137359
7 2016-09-27 14:07:20.137556
8 2016-09-27 14:07:20.137796
9 2016-09-27 14:07:20.437769
10 2016-09-27 14:07:20.437796
100 2016-09-27 14:07:23.293661 

使用
/^\s+(*Skip)(*F)|\s+/
$line=~s/^\s+$/
从行的左侧和右侧修剪空格,跳过前导空格。我经常使用它,避免了很多错误。你根本不需要跳过前导空格,因为如果你只是
split'
而不是
split/\s+/
,这是默认行为。遗憾的是,我在这里没有足够的声誉。这种方法实际上删除了前导空格,而
/^\s+(*skip)(*F)|\s+/
将保留它们。只要选择你需要的。谢谢@Tim和Wiktor,他们都在工作,但现在我将使用Tim的答案。确保您的(Wiktor)建议是正确的,并且它还帮助我获得另一个知识,因为我不熟悉使用perl:)Gah。当然,这是最好的答案。我知道有更好的解决办法,但在我的咖啡因不足的状态下,我无法把它从我的脑海中拉出来。但是如果OP在每个字段之间有多个空格,会发生什么呢?默认拆分是否能够处理此问题?是。默认情况下,拆分为“任意空白”,省略任何前导。这是因为它的行为与
awk
相同。