更新时间:2023-02-03 08:28:43
将字符串分割用空格是很简单的:
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;