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
|