Split a string into array in Perl

后端 未结 5 1853
清歌不尽
清歌不尽 2021-02-06 00:24
my $line = \"file1.gz file2.gz file3.gz\";
my @abc = split(\'\', $line);
print \"@abc\\n\";

Expected output:

file1.gz
file2.gz
file3.gz         


        
5条回答
  •  暖寄归人
    2021-02-06 00:56

    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):

    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"). 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.


    Here's an answer for the original question (with a simple string without any whitespace):

    Perhaps you want to split on .gz extension:

    my $line = "file1.gzfile1.gzfile3.gz";
    my @abc = split /(?<=\.gz)/, $line;
    print $_, "\n" for @abc;
    

    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;
    

提交回复
热议问题