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

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;

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: User Code
by chipmunk (Parson) on Dec 11, 2000 at 08:51 UTC
    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?
        You could combine both of those regexps into one by doing something like this: s#\[(?:http(s)?://)?([^\|]+)\|([^\]]+)\]#<a href="http$1://$2">$3</a>#g;

        - Brad