in reply to Re: Re: User Code
in thread User Code

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;

Replies are listed 'Best First'.
Re: Re: Re: Re: User Code
by arturo (Vicar) on Dec 10, 2000 at 01:00 UTC

    You're right. One problem is that opening paren, which doesn't have a match; another one is that I mistyped ")+" instead of "+) -- otherwise we capture one or more character next to a pipe ... that oughta learn me =)

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

      It's funny, in browsing this site and answering questions, you often pick up litle snippets that you want to include in some of your code somewhere. I used the regex that I modified to optionally allow "http://".

      Then I got to thinking - what if you want to type "https://" at the beginning?

      In my regex - it would output the links as:
      http://https://www.whatever.com
      Which is obviously no good.

      I fiddled around with it, but then got stuck and just worked it out (I think):
      $text =~ s#\[(http[s]?://)?([^\|]+)\|([^\]]+)\]#<a href="$1$2">$3 +</a>#g;
        That'll work, but I do want to point out one thing: you don't need a character class for a single character. :) $text =~  s#\[(https?://)?([^\|]+)\|([^\]]+)\]#<a href="$1$2">$3</a>#g;