Rather than explain the following snippet should give you an idea of what I'm trying to do. I just can't quite get it going and respectfully ask for the help of the monks.
Rather than explain it, here is my
incomplete and
untested (for lack of test data) solution:
sub replace {
my ($tag,$attr,$content) = @_;
$attr ||= $content;
if ($tag eq 'b') {
return "<b>$content</b>";
}
elsif ($tag eq 'url') {
return "<a href='$attr'>$content</a>";
}
# etc. etc. etc.
}
s/\[(\w+)=?(\w*)\](\w+)\[\1\]/replace($1,$2,$3)/esg;
Hope this helps :-)
Update:
Ok, let's explain it a little anyway: I've turned your problem inside out, so you only need 1 regex and only 1 s statement. This has 3 advantages:
- You don't have to run an additional s statement on the whole text for each new tag you decide to add.
- You only need to update and debug 1 regular expression if you change the overall syntax.
- You get rid of the $1 - problem.
You might need a little finetuning on this code (I haven't tested it except for syntax) but I think this is a lot easier and maintainable way of solving your problem.
--
Joost downtime n. The period during which a system
is error-free and immune from user input.