in reply to Re: Replace placeholders in text in an elegant manner
in thread Replace placeholders in text in an elegant manner

You're right on the money!!
Couldn't be more precise!!

You're way of solution realy amazed me. Thank you very much.

PS: I loved your Geoffrey James quotation too!
  • Comment on Re^2: Replace placeholders in text in an elegant manner

Replies are listed 'Best First'.
Re^3: Replace placeholders in text in an elegant manner
by GrandFather (Saint) on Feb 28, 2010 at 00:15 UTC

    There is a gotcha with the solution as written (well, several actually) - you need to provide every possible (or at least likely) case variation for each of the matched words. Combining the special markup you have alluded to elsewhere with the e flag for the regex gives a somewhat more robust solution:

    use strict; use warnings; my $text = join '', <DATA>; my %replacements = ( 'romeo' => 'CountZero', 'juliet' => 'Elisabeth', 'rom.' => 'CountZero', 'jul.' => 'Elisabeth', 'abram' => 'Igor', 'abr.' => 'Igor', ); $text =~s/%%([^%]+)%%/replace($1)/ge; print $text; sub replace { my ($needle) = @_; return "%%$needle%%" if ! exists $replacements{lc $needle}; return $replacements{lc $needle}; } __DATA__ (follows the full Project Gutenberg e-text of Shakespeare's %%Romeo%% +and %%Juliet%%; found at http://www.gutenberg.org/dirs/etext97/1ws1610.txt)

    Prints:

    (follows the full Project Gutenberg e-text of Shakespeare's CountZero +and Elisabeth; found at http://www.gutenberg.org/dirs/etext97/1ws1610.txt)

    True laziness is hard work