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/04/27 16:56:49
perl 正则表达式s///的2个疑问/usr/perl/binuse warnings;use strict;my $in=$ARGV[0];$out=$in;$out=~s/(\.w+)?$/\.out/; print $out;open LOG1,

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"; 这里加不加反斜杠都可以,但是推荐是不加的.