Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Creating links

by htmanning (Pilgrim)
on Mar 17, 2010 at 18:41 UTC ( #829242=perlquestion: print w/replies, xml ) Need Help??
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 (Pope) 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 (Friar) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://829242]
Approved by toolic
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-05-23 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?