Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

Ok, this should be a simple one...

Say I have the following line of text:
Hello.  Blablabla, go to this site called [link=http://www.something.com]Something[/link], with some more text here.
How could I change that into:
Hello.  Blablabla, go to this site called <A HREF= "http://www.something.com">Something</A>, with some more text here.

Also, a simpler version for converting the following:
Look at this image: [img]theimage.jpg[/img]
to..
Look at this image: <IMG SRC= "theimage.jpg">

Replies are listed 'Best First'.
Re: User Code
by elusion (Curate) on Dec 09, 2000 at 07:43 UTC
    I think something like this would work:
    $text =~ s~[link=(\[\w/\:\.]+)\](.+)\[/link\]~<a href='$1'>$2</a>~g;
    or
    $text =~ s~\[img\](.+)\[/link\]~<img src='$1'>~g;

    - p u n k k i d
    "Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

    update: thanx to arturo for noticing my error with the brackets

      A problem with your regex, as it stands : you need to escape some of those square brackets, as those have special meaning within regexes.

      Monolith-O, the syntax you're using is so close to HTML as it is that it doesn't seem to simplify things much. If you can change it, consider doing so. you could do something like what perlmonks does, and let [www.foo.com|the Foo site] stand in for <a href="http://www.foo.com">the Foo site</a>. A regex to match that would be:

      $text =~ s#(\[([^|])+\|([^\]]+)]#<a href="http://$1">$2</a>#g;

      Update that's wrong, as $code_or_die points out.

      Sticking with the current setup, the first regex should be something like this (untested):

      $text =~ s#\[link=(["'])(\S+)\1]([^[])+\[/link]#<a href="$2">$3</a>#g;

      This says "when you find "link=" followed by a single or double quote, grab 1 or more characters that aren't a single or double quote (whichever matched) -- store that in $2 -- followed by the single or double quote, then grab one or more characters that aren't [ and store them in $3, finished up by [/link]. Replace that whole sequence with <a href="$2">$3</a>

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        Your perlmonks-style regex didn't seem to work when I tried it. I think that the first half of it was matching the wrong thing. However this worked for me:

        $text =~ s#\[([^\|]+)\|([^\]]+)\]#<a href="http://$1">$2</a>#g;
        I also cleaned it up to allow people to put "http://" at the beginning of the URL (i.e. if they copy and paste a link):
        $Details =~ s#\[(http://)?([^\|]+)\|([^\]]+)\]#<a href="http://$2">$3 +</a>#g;
        I'm mainly looking for flexability rather than simplicity right now as I am getting used to the general code and syntax.
Re: User Code
by eg (Friar) on Dec 10, 2000 at 14:51 UTC

    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.

      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.