Perl:将字符串拆分为所需的部分
Perl : Split the string into desirable parts
我是 perl 的新手,正在尝试将这个字符串分成 4 个部分并将其存储在 @parts
数组中。
$string = " Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map Phone: 650-253-0000 Fax: 650-253-0001 Website: http://www.google.com ";
这样 @parts
数组应该变成
$part[0]= "Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map" ;
$part[1]= "Phone: 650-253-0000 ";
$part[2]= "Fax: 650-253-0001 ";
$part[3]= "Website: http://www.google.com";
如何实现?
只需根据存在于一个或多个非space 字符之前的space 进行拆分,然后是一个冒号,然后是一个space。所以这不会匹配 http:
之前存在的 space,因为 http:
之后没有 space。
my $string = "Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map Phone: 650-253-0000 Fax: 650-253-0001 Website: http://www.google.com ";
my @abc = split /\s+(?=[^:\s]+:\s+)/, $string;
print $_, "\n" for @abc;
输出:
Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map
Phone: 650-253-0000
Fax: 650-253-0001
Website: http://www.google.com
我是 perl 的新手,正在尝试将这个字符串分成 4 个部分并将其存储在 @parts
数组中。
$string = " Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map Phone: 650-253-0000 Fax: 650-253-0001 Website: http://www.google.com ";
这样 @parts
数组应该变成
$part[0]= "Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map" ;
$part[1]= "Phone: 650-253-0000 ";
$part[2]= "Fax: 650-253-0001 ";
$part[3]= "Website: http://www.google.com";
如何实现?
只需根据存在于一个或多个非space 字符之前的space 进行拆分,然后是一个冒号,然后是一个space。所以这不会匹配 http:
之前存在的 space,因为 http:
之后没有 space。
my $string = "Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map Phone: 650-253-0000 Fax: 650-253-0001 Website: http://www.google.com ";
my @abc = split /\s+(?=[^:\s]+:\s+)/, $string;
print $_, "\n" for @abc;
输出:
Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 United States - Map
Phone: 650-253-0000
Fax: 650-253-0001
Website: http://www.google.com