in reply to Re: Swap foo and bar in text?
in thread Swap foo and bar in text?
Assuming "foo" and "bar" are metasyntactic variables, and that the real substrings could be anything, the use of quotemeta is in order:
UPDATE: This paragraph should read…
Assuming "foo" and "bar" are metasyntactic variables representing literal substrings, and that the real literal substrings could be any literal substrings (not regular expression patterns), then the use of quotemeta is in order:
#!/usr/bin/perl use strict; use warnings; my %edits = ( '***' => 'stars', '|||' => 'stripes' ); my $value = '*** and |||'; my $match = '(' . join('|', map { quotemeta } keys %edits) . ')'; $value =~ s/$match/$edits{$1}/g; print $value; # Prints "stars and stripes"
(I realize this example is no longer swapping anything. Sorry.)
Jim
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Swap foo and bar in text?
by GrandFather (Saint) on Apr 20, 2012 at 08:33 UTC | |
by Jim (Curate) on Apr 20, 2012 at 15:51 UTC |
In Section
Seekers of Perl Wisdom