免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 16511 | 回复: 4
打印 上一主题 下一主题

perl里面用啥命令截取一个字符串的最后几个字符呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-22 16:52 |只看该作者 |倒序浏览

论坛徽章:
0
2 [报告]
发表于 2010-02-22 17:02 |只看该作者
这个. 模式匹配应该可以的

  1. my $str = "http://a.com/fs/900/1/5d4d9bcfc3 ... e/setup-365rili.exe";
  2. my ($result) = $str =~ /\.([^\.]+)$/ ;
  3. print $result;
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-02-22 17:31 |只看该作者
1,index
position = index (string, substring, position)
返回子串substring在字符串string中的位置,如果不存在则返回-1。参数position是可选项,表示匹配之前跳过的字符数,或者说从该位置开始匹配。

例子如下:
[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a"); print $rev,"\n";'
2
[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a", 1); print $rev,"\n";'
2
[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a", 3); print $rev,"\n";'
6
[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a", 7); print $rev,"\n";'
-1


2,rindex
position = rindex (string, substring, position)
与index类似,区别是从右端匹配。

例子如下:

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 11); print $rev,"\n";'
2
[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 12); print $rev,"\n";'
12
[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 1); print $rev,"\n";'
-1
[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 2); print $rev,"\n";'
2


3,length
num = length (string)
返回字符串长度,或者说含有字符的数目。

例子如下:

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=length($_); print $rev,"\n";'
17
[root@localhost ~]# echo -n '/var/ftp/tesa/123  ' | perl -ne '$rev=length($_); print $rev,"\n";'
19


4,substr
substr (expr, skipchars, length)
抽取字符串(或表达式生成的字符串)expr中的子串,跳过skipchars个字符,或者说从位置skipchars开始抽取子串(第一个字符位置为0),子串长度为length,

此参数可忽略,意味着取剩下的全部字符。
当此函数出现在等式左边时,expr必须为变量或数组元素,此时其中部分子串被等式右边的值替换。

substr() 函数的作用有两个:替换一部分子串。 删除一部分子串。

例子如下:

[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9,);print $rev,"\n";'
test/123
[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9, 4);print $rev,"\n";'
test

替换:
[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9, 4)="hello"; print $rev,"\n";'
hello
删除:
[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9, 4)=""; print $rev,"\n";'




5,lc,uc,lcfirst,ucfirst
lc,将字符串改为小写
uc,将字符串改为大写
lcfirst,改变字符串首字母小写
ucfirst,改变字符串首字母大写

例子如下:

[root@localhost ~]# echo -n 'hello, hanli' | perl -ne '$rev=uc($_); print $rev,"\n";'
HELLO, HANLI
[root@localhost ~]# echo -n 'HELLO, hanli' | perl -ne '$rev=lc($_); print $rev,"\n";'
hello, hanli
[root@localhost ~]# echo -n 'hello, Hanli' | perl -ne '$rev=ucfirst($_); print $rev,"\n";'
Hello, Hanli
[root@localhost ~]# echo -n 'hello, Hanli' | perl -ne '$rev=lcfirst($_); print $rev,"\n";'
hello, Hanli

文章出处:DIY部落(http://www.diybl.com/course/6_sy ... 081117/151770.html#)

论坛徽章:
0
4 [报告]
发表于 2010-02-22 18:31 |只看该作者
index

论坛徽章:
0
5 [报告]
发表于 2010-02-22 18:42 |只看该作者
index
shaneqi 发表于 2010-02-22 18:31



$a1 = "http://a.com/fs/900/1/5d4d9bcfc3883584a7010e85af3f089829165094/exe/setup-365rili.exe";
$p = rindex($a1,".");
print "$p \n";
$res = substr($a1,$p,length($a1)-$p);
print "$res \n";
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP