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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: User Code
by $code or die (Deacon) on Dec 11, 2000 at 07:19 UTC
    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;
        Good point! :)

        I also realised that it will only work if you type http:// or https://. It's nice to be able to leave out the http:// bit if you want it to default to http://

        So maybe you would have to do two regexes:
        $text =~ s#\[https://([^\|]+)\|([^\]]+)\]#<a href="https://$1">$2</a> +#g; $text =~ s#\[(http://)?([^\|]+)\|([^\]]+)\]#<a href="http://$2">$3</a +>#g
        Is there a simpler way of doing that?