perl 正则表达式s///的2个疑问/usr/perl/binuse warnings;use strict;my $in=$ARGV[0];$out=$in;$out=~s/(\.w+)?$/\.out/; print $out;open LOG1,"$out"; $out=~s/(\.w+)?$/\.out/; 1.这一行为什么必须加上?才能匹配并且替换成.out,$out=~s
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/01 07:56:51
![perl 正则表达式s///的2个疑问/usr/perl/binuse warnings;use strict;my $in=$ARGV[0];$out=$in;$out=~s/(\.w+)?$/\.out/; print $out;open LOG1,](/uploads/image/z/8470510-70-0.jpg?t=perl+%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8Fs%2F%2F%2F%E7%9A%842%E4%B8%AA%E7%96%91%E9%97%AE%2Fusr%2Fperl%2Fbinuse+warnings%3Buse+strict%3Bmy+%24in%3D%24ARGV%5B0%5D%3B%24out%3D%24in%3B%24out%3D%7Es%2F%28%5C.w%2B%29%3F%24%2F%5C.out%2F%3B+print+%24out%3Bopen+LOG1%2C%22%24out%22%3B+%24out%3D%7Es%2F%28%5C.w%2B%29%3F%24%2F%5C.out%2F%3B+1.%E8%BF%99%E4%B8%80%E8%A1%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BF%85%E9%A1%BB%E5%8A%A0%E4%B8%8A%3F%E6%89%8D%E8%83%BD%E5%8C%B9%E9%85%8D%E5%B9%B6%E4%B8%94%E6%9B%BF%E6%8D%A2%E6%88%90.out%2C%24out%3D%7Es)
perl 正则表达式s///的2个疑问/usr/perl/binuse warnings;use strict;my $in=$ARGV[0];$out=$in;$out=~s/(\.w+)?$/\.out/; print $out;open LOG1,"$out"; $out=~s/(\.w+)?$/\.out/; 1.这一行为什么必须加上?才能匹配并且替换成.out,$out=~s
perl 正则表达式s///的2个疑问
/usr/perl/bin
use warnings;
use strict;
my $in=$ARGV[0];
$out=$in;
$out=~s/(\.w+)?$/\.out/;
print $out;
open LOG1,"$out";
$out=~s/(\.w+)?$/\.out/;
1.这一行为什么必须加上?才能匹配并且替换成.out,$out=~s/(\.w+)$/\.out/; 为什么不能替换
2.\.out--替换成的内容为什么不加反斜杠也可以呢?
perl 正则表达式s///的2个疑问/usr/perl/binuse warnings;use strict;my $in=$ARGV[0];$out=$in;$out=~s/(\.w+)?$/\.out/; print $out;open LOG1,"$out"; $out=~s/(\.w+)?$/\.out/; 1.这一行为什么必须加上?才能匹配并且替换成.out,$out=~s
他这个替换的作用是把 一个文件名例如 a.txt 替换成 a.out
或者无后缀的文件名 b 替换为 b.out
所以你的问题:
1. 加上? 是为了可以匹配有后缀和无后缀的两种情况,还有你的正则写错了 w 前面应该加个\
写成 s/(\.\w+)?$/.out/ 才对
2. s后面的替换内容不是正则表达式,是一个字符串形式,是不会理解+? 这种东西的,不需要转义
字符串里面怎么写,那里就怎么写
就像 print "\.out"; 这里加不加反斜杠都可以,但是推荐是不加的.