在Perl中使用regex时,tr///和s///有什么区别? [英] What's the difference between tr/// and s/// when using regex in Perl?

查看:214
本文介绍了在Perl中使用regex时,tr///和s///有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Perl中使用正则表达式时,应该在tr///上使用s///吗?

I was wondering when one should use s/// over tr/// when working with regular expressions in Perl?

推荐答案

s///用于替代:

$string =~ s/abc/123/;

这会将在$string中找到的第一个"abc"替换为"123".

This will replace the first "abc" found in $string with "123".

tr///用于音译:

$string =~ tr/abc/123/;

这会将$string中所有出现的"a"替换为"1",所有出现的"b"替换为"2",并将所有出现的"c"替换为"3".

This will replace all occurrences of "a" within $string with "1", all occurrences of "b" with "2", and all occurrences of "c" with "3".

这篇关于在Perl中使用regex时,tr///和s///有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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