Perl——正则表达式(四) 查找替换s///
来源:互联网 发布:网络视频直播平台排行 编辑:程序博客网 时间:2024/06/06 01:05
一. 介绍
使用 s/regex/replacement/modifiers
进行查找替换
二. 实例
(1) s///
$f = "'quoted words'";#进行模式匹配,下面方法去除''单引号if($f =~ s/^'(.*)'$/$1/) { #true, $1指的是引用了第一组(.*)的内容, ^$这两个字符用来表示开始与结束print "matches","\n"; # mathcesprint $f,"\n"; # quoted words # 注意 标量$f 匹配后本身内容发生了变化}
(2) s///r
用它进行匹配后,原始标量的值不会发生变化,可以把新值赋值给一个新的标量
$f = "'quoted words'";#进行模式匹配,下面方法去除''单引号$n = $f =~ s/^'(.*)'$/$1/r;print "matches","\n";print $f,"\n"; # quoted words # 注意 标量$f 匹配后本身内容无变化print $n,"\n"; # 'quoted words' # 注意 $n
(3) s///g 多次查找替换
$z = "time hcat to feed the cat hcat";$z =~ s/cat/AAA/g;#替换多次print $z,"\n"; #结果为 time hAAA to feed the AAA hAAA
(4) s///e 求值
# reverse all the words in a string$x = "the cat in the hat";$x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"# convert percentage to decimal$x = "A 39% hit rate";$x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"
(5) s/// 可以用 s!!! , s{}{} , s{}// 进行替换
0 0
- Perl——正则表达式(四) 查找替换s///
- Perl 正则表达式替换
- 正则表达式查找替换
- 用于“查找和替换”的正则表达式(四)
- learning perl(四) 正则表达式
- Dreamweaver 正则表达式 查找替换
- Vim正则表达式查找替换
- VS2005 正则表达式查找替换
- eclipse 正则表达式查找替换
- Vim正则表达式查找替换
- Vim查找替换 & 正则表达式
- xCode正则表达式替换查找
- editplus查找替换 正则表达式
- MyEclipse 正则表达式查找替换
- perl学习小结(3):s///匹配替换及正则表达式高级应用
- 巧用多个正则表达式解决取反替换问题(解决不匹配则替换问题)——用sed和perl的正则表达式
- Perl之正则表达式基础(四)
- 【perl】学习笔记(四)--正则表达式
- SQLite可视化管理工具汇总
- IntentFilter
- HTML5教程:第二章 HTML5的常用元素与属性---文本格式化元素
- #define、const以及sizeof
- 算法导论第七章快速排序的C++实现
- Perl——正则表达式(四) 查找替换s///
- JDBC中Statement与PreparedStatement的区别
- jquery file upload 后台收到的文件名中文乱码, filename中文乱码
- centos 完全移除mysql
- C++中inline函数的使用
- 1410282310-hd-Seinfeld
- 第9周项目6-2换分币问题
- 视频采集模块设计
- C# ServerManager IIs 7