Tim Sargent wrote:
> <snip> I'd prefer an efficient and neat method to do all this,
> but am rather stuck. Any pointers that can get me started down the right
> path would be greatly appreciated...I know there's an answer out there, I'm
> just having a devil of a time finding it. Sorry for the lengthy post.
I would store all the possible transformations in an array, containing
the regex and the replacement string. the iterate over the array and
replacing each occurences as they are found:
As for avoiding repeating replacement, I just tweaked the 'green' regex a
little bit by adding a (?!\.p) at the end ( look-ahead negative
assertion: the string '.p' must not be found after /GS\d/ )
#!/usr/bin/perl -w
use strict;
use 5.6.0;
my @transfo = (
[ qr/\b(GS\d{1,2}\.p\d+-\d+\b)/, 'green-atu' ],
[ qr/\b(GS\d{1,2})(?!\.p)\b/, 'green' ],
[ qr/\b(launch)\b/i, 'launch' ],
[ qr/\b([BS]\d{1,2})\b/, 'blue' ],
);
my $text = <<'_TEXT_';
GS5 launches 3 plasma torpedoes (GS5.p1-3),
warhead strength 20, speed 32
_TEXT_
foreach my $case (@transfo){
$text =~ s{$case->[0](?!<span>)}
{<span class="$case->[1]">$1</span>}ig;
}
print "Result:\n$text";
__END__
Result:
<span class="green">GS5</span> launches 3 plasma torpedoes (<span
class="green-atu">GS5.p1-3</span>), warhead strength 20, speed 32
--
briac
Two leaping Dahuts.
A thrush flying towards the
east. Two bears waiting.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]