Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

RE: Regex to find URLs in a string

by FouRPlaY (Monk)
on Oct 25, 2000 at 03:41 UTC ( [id://38256]=note: print w/replies, xml ) Need Help??


in reply to Regex to find URLs in a string

Here's a sub I wrote (it works) to change URLs to include the tags to be outputed to HTML. You might be able to fool around with it.
sub urlcheck($newline) { $newline =~ s/http\:\/\//\<a href \= \"http\:\/\//ig; if (substr ($newline, $#newline - 1, 1) eq ".") { $x = substr ($newline, /\G/, $#newline - 1); } else { $x = substr ($newline, /\G/); } $page = $x; $page =~ s/(http\:\/\/)|(\<\/a\>)|\=|\"|(href)|(\<a)|(\w+[ ]+)//g; $newline =~ s/$x/$x\"\>$page\<\/a\>/; return $newline; }
BTW, it checks to see if the URL is at the end of a sentence and therefore might have an extra period.

Replies are listed 'Best First'.
RE: RE: Regex to find URLs in a string
by merlyn (Sage) on Oct 25, 2000 at 03:46 UTC
      Simple, I didn't know it existed! Also, I didn't know about Net::Finger, and I wrote a script to do that; also HTML::FromText, and I spent many a week programing a script to do that too!
RE: RE: Regex to find URLs in a string
by mdillon (Priest) on Oct 25, 2000 at 21:57 UTC

    i don't think that $#newline means what you think it means. that variable indicates the index of the last element of @newline (if it is non-empty) and has nothing to do with the scalar variable $newline.

    try the following instead:

    if (substr($newline, -1) eq '.')
      Thanks for the correction. I've learn most of my PERL by guessing. I figured if $# worked for arrays, it might work for scalars.

      Your suggestions is also, I find, a lot clearer and more percise. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://38256]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-23 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found