Sunday, 1 January 2023

Without changing the original,how do we perform a replacement on a string using perl regex?



Method1:
 
(my $newstring = $oldstring) =~ s/foo/bar/g;

Method2:
 
my $newstring = $oldstring =~ s/foo/bar/gr;   

Method3:
 
if it is an arrays:
my @a = ('appl', 'appl bal', 'fruit?');
my @b = map { s/appl/apple/; $_ } map { $_ } @a;
 

Labels: , , , , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home