Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://829242]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-12-16 17:01 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (71 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!