http://www.perlmonks.org?node_id=829242

htmanning has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the following to automatically take a domain name, and turn it into a link:
$text =~ s!([^\s]+\.com)!<a href="http://www.$1" target="_new">$1</a>! +gi;
But what if I have something like
www.test.com/thisandthat/this.html
How do I parse that entire link and make it a hyperlink?

Replies are listed 'Best First'.
Re: Creating links
by Corion (Patriarch) on Mar 17, 2010 at 18:49 UTC
Re: Creating links
by Dru (Hermit) on Mar 17, 2010 at 19:38 UTC
Re: Creating links
by Cody Fendant (Hermit) on Mar 18, 2010 at 04:51 UTC
    Not all website URLs begin with "www".
Re: Creating links
by 7stud (Deacon) on Mar 17, 2010 at 19:41 UTC

    1) ^\s is equivalent to \S

    2) Just because you can do something doesn't mean you should. The delimiter ! is a horrible choice.

    3) I think this would handle the case you mentioned:

    s{(www\.)?(\S\.com)}{<a href="http://www.$2" target="_new">/>$2</a>}

    4) Don't try to reinvent the wheel. If there is a module that deals with all the intricacies of parsing something, use it.