拆分字符串转换成在Perl数组 [英] Split a string into array in Perl
问题描述
my $line = "file1.gz file1.gz file3.gz";
my @abc = split('',$line);
print "@abc\n";
期望的输出:
file1.gz
file2.gz
file3.gz
我想要的输出为 file1.gz
在 $ ABC [0]
和 $ ABC [1]
应该有 file2.gz
; $ ABC [2]
应该有 file3.gz
。我该如何分割?
I want the output to be file1.gz
in $abc[0]
and $abc[1]
should have file2.gz
; $abc[2]
should have file3.gz
. How do I split?
推荐答案
将字符串分割用空格是很简单的:
Splitting a string by whitespace is very simple:
print $_, "\n" for split ' ', 'file1.gz file1.gz file3.gz';
这是拆分
实际上是一种特殊形式(如该功能通常需要模式,而不是字符串):
This is a special form of split
actually (as this function usually takes patterns instead of strings):
作为另一种特殊情况,拆分
模拟的默认行为
命令行工具 AWK
在 PATTERN
是省略或文字
单个空格字符组成的字符串(如或
\\ X20
)。在这种情况下,在 EXPR
任何前导空格是
除去之前发生分裂,而 PATTERN
作为代替处理
如果它是 / \\ s + /
;特别是,这意味着任何连续
空白(不只是单个空格字符)用作隔膜。
As another special case,
split
emulates the default behavior of the command line toolawk
when thePATTERN
is either omitted or a literal string composed of a single space character (such as' '
or"\x20"
). In this case, any leading whitespace inEXPR
is removed before splitting occurs, and thePATTERN
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.
下面是原问题的答案(用一个简单的字符串,而没有任何空格):
Here's an answer for the original question (with a simple string without any whitespace):
也许你想拆就。广州
扩展:
my $line = "file1.gzfile1.gzfile3.gz";
my @abc = split /(?<=\.gz)/, $line;
print $_, "\n" for @abc;
下面我用(小于?= ...)
结构,这是的查找背后断言,基本上在按。广州
子pceded行$ p $每个点使分裂。
Here I used (?<=...)
construct, which is look-behind assertion, basically making split at each point in the line preceded by .gz
substring.
如果您与固定集扩展的工作,你可以扩展模式,包括他们所有的:
If you work with the fixed set of extensions, you can extend the pattern to include them all:
my $line = "file1.gzfile2.txtfile2.gzfile3.xls";
my @exts = ('txt', 'xls', 'gz');
my $patt = join '|', map { '(?<=\.' . $_ . ')' } @exts;
my @abc = split /$patt/, $line;
print $_, "\n" for @abc;
这篇关于拆分字符串转换成在Perl数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!