PHP有explode函数,Python, Ruby and JavaScript 都有各自的分割(split)方式. 在perl中有split函数
文章来源:(https://perlmaven.com/perl-split)
1 Syntax of split
split REGEX, STRING 按照正则表达式(regex)分割字符串
split REGEX, STRING, LIMIT 可以最多分割成LIMIT个块
split REGEX 如果没给字符串,默认是$_
split 没有任何参数,$_用/\s+/作为正则表达式
2 Simple cases
split 返回一个字符串列表:
use Data::Dumper qw(Dumper);
my $str = "ab cd ef gh ij";
my @words = split / /, $str;
print Dumper \@words;
result:
$VAR1 = [
'ab',
'cd',
'ef',
'gh',
'ij'
];
3 Limit the number of parts
当limit = 2 时,split返回两个参数
use Data::Dumper qw(Dumper);
my $str = "ab cd ef gh ij";
my @words = split / /, $str, 2;
print Dumper \@words;
result:
$VAR1 = [
'ab',
'cd ef gh ij'
];
4 Assign to scalars
我们可以将结果分配给一个标量变量列表,而不是将结果分配给单个数组
方法一
my $str = "root:*:0:0:System Administrator:/var/root:/bin/sh";
my ($username, $password, $uid, $gid, $real_name, $home, $shell) = split /:/, $str;
print "$username\n";
print "$real_name\n";
result:
root
System Administrator
方法二
my $str = "root:*:0:0:System Administrator:/var/root:/bin/sh";
my @fields = split /:/, $str;
my $username = $fields[0];
my $real_name = $fields[4];
print "$username\n";
print "$real_name\n";
result:
root
System Administrator
方法三 使用数组切片
my $str = "root:*:0:0:System Administrator:/var/root:/bin/sh";
my @fields = split /:/, $str;
my ($username, $real_name) = @fields[0, 4];
#等价于 my ($username, $real_name) = (split /:/, $str)[0, 4];
print "$username\n";
print "$real_name\n";
result:
root
System Administrator
5 Split on more complex regex
用更复杂的正则分割
use Data::Dumper;
my $str="fname = Foolname = Baremail=foo@bar.com";
my @array=split/=/,$str;
print Dumper(\@array);
result:
$VAR1 = [
'fname ',
' Foolname ',
' Baremail',
'foo.com'
];
上面的结果有什么不妥的地方呢?我们发现分成的四部分中有空格的存在,而空格未必是我们想要的。 所以我们要对程序进行改进
use Data::Dumper;
my $str="fname = Foolname = Baremail=foo@bar.com";
my @array=split/\s*=\s*/,$str;
print Dumper(\@array);
result:
$VAR1 = [
'fname',
'Foolname',
'Baremail',
'foo.com'
];
6 Split on multiple characters 多种字符分割
我们有这么一个字符串”[email protected]”我们既想用“=”分割,也想 用“&”分割。怎么做到呢?
use Data::Dumper;
my $str='[email protected]';
#注意这里是单引,若是双引号 [email protected]打印出来就是foo.com 下面同理
my @array=split/[=&]/,$str;
print Dumper(\@array);
result:
$VAR1 = [
'fname',
'Foo',
'lname',
'Bar',
'email',
'[email protected]'
];;
当然,如果我们知道这些是键-值对,那么我们可能希望将结果分配给哈希而不是数组
use Data::Dumper;
my $str='[email protected]';
my %array=split /[=&]/,$str;
print Dumper(\%array);
$VAR1 = {
'lname' => 'Bar',
'fname' => 'Foo',
'email' => '[email protected]'
};
use Data::Dumper qw(Dumper);
my $str = "Hello World";
my @chars = split //, $str;
print Dumper \@chars;
result:
$VAR1 = [
'H',
'e',
'l',
'l',
'o',
' ',
'W',
'o',
'r',
'l',
'd'
];
Beware of regex special characters
A common pitfall with split, especially if you use a string as the separator (split STRING, STRING) as in split ‘;’, $line; is that even if you pass the first parameters as a string it still behaves as a regex. So for example
split '|', $line; 等价于 split /|/, $line;
and both will split the string character by character. The right way to split on a pipe | character is to escape the special regex character:
split /\|/, $line;