我正在编写一个使用 Getops 接受用户输入的小程序,基于它,该程序将尝试将模式与某些文本进行匹配,或者用文本替换匹配的内容。
我遇到的问题是我无法让替换部分工作。我正在查看手册页中的 qr// 条目:http: //perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators但我没有任何运气。在这种情况下,我尝试完全像文档一样对我的代码进行建模。我编译了一个匹配模式,并将其替换为替换。
有人能指出我哪里出错了吗?(不要太担心安全问题,这只是个人使用的一个小脚本)
这是我正在查看的内容:
if($options{r}){
my $pattern = $options{r};
print "\nEnter Replacement text: ";
my $rep_text = <STDIN>;
#variable grab, add flags to pattern if they exist.
$pattern .= 'g' if $options{g};
$pattern .= 'i' if $options{i};
$pattern .= 's' if $options{s};
#compile that stuff
my $compd_pattern = qr"$pattern" or die $@;
print $compd_pattern; #debugging
print "Please enter the text you wish to run the pattern on: ";
my $text = <STDIN>;
chomp $text;
#do work and display
if($text =~ s/$compd_pattern/$rep_text/){ #if the text matched or whatever
print $text;
}
else{
print "$compd_pattern on \n\t{$text} Failed. ";
}
} #end R FLAG
当我使用 -r "/matt/" -i 运行它并在文本“matt”上输入替换文本“matthew”时,它失败了。为什么是这样?
编辑:
谢谢你们的答案!这真的很有帮助。我将您的两个建议都结合到了该问题的可行解决方案中。我必须稍微不同地处理 /g 标志。这是工作示例:
if($options{r}){
my $pattern = $options{r};
print "\nEnter Replacement text: ";
my $rep_text = <STDIN>;
chomp $rep_text;
#variable grab, add flags to pattern if they exist.
my $pattern_flags .= 'i' if $options{i};
$pattern_flags .= 's' if $options{s};
print "Please enter the text you wish to run the pattern on: ";
my $text = <STDIN>;
chomp $text;
#do work and display
if($options{g}){
if($text =~ s/(?$pattern_flags:$pattern)/$rep_text/g){ #if the text matched or whatever (with the g flag)
print $text;
}
else{
print "$pattern on \n\t{$text} Failed. ";
}
}
else{
if($text =~ s/(?$pattern_flags:$pattern)/$rep_text/){ #if the text matched or whatever
print $text;
}
else{
print "$pattern on \n\t{$text} Failed. ";
}
}
} #end R FLAG