How can I extract URLs from plain text with Perl?

Posted by Russell C. on Stack Overflow See other posts from Stack Overflow or by Russell C.
Published on 2010-04-02T01:51:27Z Indexed on 2010/04/02 2:03 UTC
Read the original article Hit count: 394

Filed under:
|
|
|

I need the Perl regex to parse plain text input and convert all links to valid HTML HREF links. I've tried 10 different versions I found on the web but none of them seen to work correctly. I also tested other solutions posted on StackOverflow, none of which seem to work. The correct solution should be able to find any URL in the plain text input and convert it to:

<a href="$1">$1</a>

Some cases other regular expressions I tried didn't handle correctly include:

  1. URLs at the end of a line which are followed by returns
  2. URLs that included question marks
  3. URLs that start with 'https'

I'm hoping that another Perl guy out there will already have a regular expression they are using for this that they can share. Thanks in advance for your help!

© Stack Overflow or respective owner

Related posts about perl

Related posts about regex