It's Friday afternoon and my brain is fried.

I have a boatload of plain text that contains things that look like http links. If I find one, then I want to wrap it in HTML anchors.

I don't think there's a module that does this, but I welcome suggestions. Otherwise, the spec is in the test below. If someone could show me the error of my ways I'd be a happy man.

Bonus points for converting the first link (http://www.example.com) to http://www.example.com/ in the HREF (add the omitted slash).

use strict; use warnings; my $html = <<END_HTML; Blah blah blah Web: http://www.example.com info.example.com Web: http://info.example.com/ And another Web site: http://another.example.com/ (doesn't always work) Nice blog here: http://blog.example.com/niceblog/ END_HTML my $target = <<END_HTML; Blah blah blah Web: <a href="http://www.example.com">www.example.com</a> job.example.com Web: <a href="http://info.example.com/">info.example.com</a> And another Web site: <a href="http://another.example.com/">another.example.com</a +> (doesn't always work) Nice blog here: <a href="http://blog.example.com/niceblog/">blog.examp +le.com</a> END_HTML use Test::More tests => 1; $html =~ s{(http://(\S+)(?:/\S*)?)}{<a href="$1">$2</a>}g; is($html, $target);

• another intruder with the mooring in the heart of the Perl


In reply to Linkifying URLs in plain text by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.