Matching URls

J

JR

Seansan said:
Hi,

Does anyone know of a link to or an example of a decent regexp that wil
recognize internet URLs? (It needs to match urls starting with http:// and
www.)

I am trying to replace a string like
"My Homepage is @ http://www.homepage.nl"
with
"My Homepage is @ <A HREF =
'http://www.homepage.nl'>http://www.homepage.nl</A>"

Thx, Seansan

## I think this is what you want. Good luck.
## JR

#!/perl
use strict;
use diagnostics;
use warnings;

while(<DATA>) {
s/(http:\/\/www.*)\b/<a href='$1'>$1<\/a>/g;
print $_, "\n";
}

__DATA__
My homepage is @ http://www.homepage.nl
My favorite site is @ http://www.espn.com
My second most favorite site is @ http://www.whatever.org

=pod
## OUTPUT

My homepage is @ <a href='http://www.homepage.nl'>http://www.homepage.nl</a>
My favorite site is @ <a
href='http://www.espn.com'>http://www.espn.com</a>
My second most favorite site is @ <a
href='http://www.whatever.org'>http://www.whatever.org</a>

=cut
 
B

Bob Walton

Seansan wrote:

....

Does anyone know of a link to or an example of a decent regexp that wil
recognize internet URLs? (It needs to match urls starting with http:// and
www.) ....


Thx, Seansan

You could:

use Regexp::Common::URI;
 
S

Seansan

Thx JR (and others),

I tried it, and it works. But I only have 1 problem:

*) It matches all the text after the url as well."My String: "Homepage is @
http://www.homepage.nl, yep thats it" becomes
"Homepage is @ <A HREF=http://www.homepage.nl yep thats
it>http://www.homepage.nl yep thats it</A>"

Any ideas on how to solve this? I played witht he regexp a while, but I cant
figure it out.

ps. How would I alter the regexp to match www. also? Like this?
s/([http:\/\/www|www].*)\b/<a href='$1'>$1<\/a>/g;

Seansan
 
P

Patrick LeBoutillier

Seansan said:
Hi,

Does anyone know of a link to or an example of a decent regexp that wil
recognize internet URLs? (It needs to match urls starting with http:// and
www.)

Check out the Regexp::Common module. I think it has what you are looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top