Hi, can anyone help with this little problem I have?

I'm trying to automatically hyperlink url's from some plain text and I have a simple (but not perfect) regular expression that does this.

However when I try and use it with a url that wraps onto a new line, the url itself contains a new line and stops the browser going to the right page.

Allow me to demonstrate with this bit of code:

#!/usr/bin/perl use strict; my $output = ""; open(FILE, "file.txt") || die "$!"; while(<FILE>) { $output .= $_; } close FILE; $output =~ s#(http://[^\!\"\£\$\^\*\(\)\{\}\[\]\;\:\'\@\,\<\> ]+)#\<a +href="$1" target="_blank"\>$1\</a\>#gis; print $output;
If file.txt has the following:
The quick brown fox at http://fish.org would like to go home and http: +//see through.com/pants/foo/bar what there is to eat
then my output is:
The quick brown fox at <a href="http://fish.org" target="_blank">http: +//fish.org</a> would like to go home and <a href="http://see through.com" target="_blank">http://see through.com</a> what there is to eat
So my question is, is there any way i can remove that \n that is in the middle of the a href?

Many thanks!


In reply to Automatically hyperlinking text fails with newlines by Anonymous Monk

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.