in reply to User Code

Here's another solution that uses only one very general regexp and an external handler:

while (<>) { s/\[([^=\]\/]+)(=([^\]]+))?\]([^\[]+)\[\/\1]/&handler($1,$3,$4)/eg +; print; } sub handler { my ($tag, $value, $data) = @_; for ( $tag ) { /img/ && return "<img src='$data'>"; /link/ && return "<a href='$value'>$data</a>"; } }

What's happening here is that every time perl encounters a bracketed tag, it extracts the tag name, value (if defined) and data (the text between the tags) and calls the sub handler which returns the replacement text.

An advantage here is that if you want to add more tags, you just have to edit the handler sub rather then generating a bunch of regular expressions that your text'll have to go through serially. A disadvantage is that, well, the regular expression might be a little confusing.

Replies are listed 'Best First'.
Re: Re: User Code
by Monolith-0 (Beadle) on Dec 16, 2000 at 07:05 UTC
    I finally got to try that out, and it works great eg. However, I want to go another step, but I haven't been able to figure it out on my own. I'd like to have it a bit recursive so it could convert something like the following:

    [link=somepage.html][img]someimage.jpg[/img][/link]

    The way that it is right now, that line would just have the img tag converted, but not the link.

    I seek further wisdom.