拆分空白字符和剥离空字段

时间:2013-09-26 15:07:04

标签: perl split whitespace

($red, $tapinfo) = split(/:/, $line);
@fields = split(/\s+/, $tapinfo);

在数组字段中,我看到甚至空间都被添加了。我想消除空间,以便字段只包含非空格字符。请评论可能出现的问题。

2 个答案:

答案 0 :(得分:3)

我假设您正在讨论剩余的领先空白,因此@fields看起来像:

$VAR1 = [
          '',    # empty field
          'foo',
          'bar'
        ];

这是因为当您使用默认/\s+/(单个空格字符)时,您split使用' '。此默认行为将在拆分字符串之前删除前导空格。换句话说,你应该这样做:

@fields = split(' ', $tapinfo);

perldoc -f split中记录了这一点:

As another special case, "split" emulates the default behavior
of the command line tool awk when the PATTERN is either omitted
or a *literal string* composed of a single space character (such
as ' ' or "\x20", but not e.g. "/ /"). In this case, any leading
whitespace in EXPR is removed before splitting occurs, and the
PATTERN is instead treated as if it were "/\s+/"; in particular,
this means that *any* contiguous whitespace (not just a single
space character) is used as a separator. However, this special
treatment can be avoided by specifying the pattern "/ /" instead
of the string " ", thereby allowing only a single space
character to be a separator.

答案 1 :(得分:0)

split默认

相同的内容

my @list = $string =~ /\S+/g;

即。它找到非空白字符的所有连续子串。

您可以使用正则表达式,但要从split获取默认行为,请将单个文字空格字符作为第一个参数传递。 正则表达式。文档说明了这个

  

作为另一种特殊情况,当省略PATTERN或由单个空格字符组成的文字字符串(例如''或“\ x20”,但不是例如/)时,split会模拟命令行工具awk的默认行为/)。在这种情况下,EXPR中的任何前导空格都会在拆分发生之前被删除,而PATTERN则被视为/ \ s + /;特别是,这意味着任何连续的空格(不只是一个空格字符)都被用作分隔符。