在perl中检测空行(仅包含空格)

时间:2016-01-20 12:47:07

标签: regex perl

我的程序读取了一个文件,我不想处理空行

while (<FICC>) {
my $ligne=$_;
if ($ligne =~ /^\s*$/){}else{
print " $ligne\n";}

但此代码也会打印空行

我测试的文件包含:

Ms. Ruth Dreifuss Dreifuss Federal Councillor Federal ruth
     
sir christopher warren US Secretary of state secretary of state
     
external economic case federal economic affair conference the Federal Office case
     
US bill clinton bill clinton Mr. Bush
     
Nestle food cs holding swiss Swiss Performance Index Performance

3 个答案:

答案 0 :(得分:3)

我认为这是因为您在代码中使用了\n。只需从代码中删除\n,就可以了。

通常人们从文件中读取一行后会chomp删除行尾字符。

答案 1 :(得分:3)

更简单的编写方法可能是反转逻辑,只打印包含非空白字符的行。

while (<FICC>) {
  my $ligne = $_;
  if ($ligne =~ /\S/) {
    print " $ligne"; # No need for linefeed here as $ligne already has one
  }
}

更新:使用示例数据进行演示:

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
  my $ligne = $_;

  if ($ligne =~ /\S/) {
    print " $ligne";
  }
}

__END__
Ms. Ruth Dreifuss Dreifuss Federal Councillor Federal ruth

sir christopher warren US Secretary of state secretary of state

external economic case federal economic affair conference the Federal Office case

US bill clinton bill clinton Mr. Bush

Nestle food cs holding swiss Swiss Performance Index Performance

输出:

 Ms. Ruth Dreifuss Dreifuss Federal Councillor Federal ruth
 sir christopher warren US Secretary of state secretary of state
 external economic case federal economic affair conference the Federal Office case
 US bill clinton bill clinton Mr. Bush
 Nestle food cs holding swiss Swiss Performance Index Performance

这对我来说似乎是正确的。

答案 2 :(得分:1)

原因还在于您在字符串的末尾添加了一个新行,其中已经有一个换行符“$ ligne \ n”,因此请使用 chomp ,如下所示

我认为更好的方法是使用 next (跳到下一个循环迭代),因为它会删除代码中的一些括号:

while (<FICC>) {
    my $ligne=chomp $_;
    next if $ligne =~ /^\s*$/;
    print " $ligne\n";
}