Wednesday, March 04, 2009

Perl - Find and replace without Regex

Perl doesn't have built-in string replace function as we have in C# but using \Q operator you can achieve same effect.



my $pattern = '[lerp]+';
my $target = 'this is [lerp]+ perl';
(
my $output1 = $target) =~ s/${pattern}//g;
(
my $output2 = $target) =~ s/\Q${pattern}//g;
print $output1 , "\n";
print $output2 , "\n";




Ouput:
this is []+
this is perl

No comments: