Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: Swap foo and bar in text?

by Jim (Curate)
on Apr 20, 2012 at 07:17 UTC ( #966113=note: print w/replies, xml ) Need Help??

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.)


Replies are listed 'Best First'.
Re^3: Swap foo and bar in text?
by GrandFather (Sage) on Apr 20, 2012 at 08:33 UTC
    the real substrings could be anything

    Indeed, which is why I removed the quote meta code from the example code before posting it - anything could include regular expressions which don't work so well when quoted.

    True laziness is hard work

      I don't understand what you're saying. Why did you remove quotemeta? Isn't it necessary as a precaution?

      Never mind. I figured out what you're saying.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966113]
[perldigious]: No offense to any Scotsman, I love Scots. Well actually, I love Scotch, but I'm sure the people are great too. :-P
[Discipulus]: perldigious i understand i word on ten, to be optimistic..
LanX will try to give his next LPW talk with a Cogney intonation
[Discipulus]: if you love Scotch in scotland your safety will be.. glengranted
[LanX]: *Cockney
erix tries for a few glo'al stops
[Discipulus]: or was Cognac (in|s)tonation?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-06-23 16:53 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (552 votes). Check out past polls.