如何仅在最后一次出现分隔符时拆分 Perl 字符串? [英] How can I split a Perl string only on the last occurrence of the separator?

查看:38
本文介绍了如何仅在最后一次出现分隔符时拆分 Perl 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 $str="1:2:3:4:5";我的 ($a,$b)=split(':',$str,2);

my $str="1:2:3:4:5"; my ($a,$b)=split(':',$str,2);

在上面的代码中,我使用了 limit 作为 2 ,所以 $a 将包含 1 并且其余元素将在 $b 中.像这样,我希望最后一个元素应该在一个变量中,最后一个元素之前的元素应该在另一个变量中.

In the above code I have used limit as 2 ,so $a will contain 1 and remaining elements will be in $b. Like this I want the last element should be in one variable and the elements prior to the last element should be in another variable.

示例

$str = "1:2:3:4:5" ; 
# $a should have "1:2:3:4"  and $b should have "5" 
$str =  "2:3:4:5:3:2:5:5:3:2" 
# $a should have "2:3:4:5:3:2:5:5:3" and $b should have "2"

推荐答案

split(/:([^:]+)$/, $str)

这篇关于如何仅在最后一次出现分隔符时拆分 Perl 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆