Hi,

I'm trying to create what I thought should be a rather simple regex, but I seem to be having all kinds of problems with it (due in part, maybe, to an absence from Perl for a while)

Anyway, I'm trying to identify, and then alter, URLs in given strings. The URLs and the strings will vary daily, and in quality of formatting. The URLs could be anything at all, but they are just plain URLs (i.e. no HTML tags).

There may be more than one URL in a string, and the strings may contain both http and/or https URLs.

The URLs might be followed by any character, so it's not necessarily easy to figure that bit out. The character following the URL could just as easily be a misplaced quotation mark which doesn't even belong there, or it could be a space, or new line character, etc.

For example, a string might look like any of these (among other possibilities):

a) black and white stuff http://test.com/testing" blah blah b) rain in spain blah blah https://chewu.to/x7w c) http://udsu.de/823; test this d) Just testing ... http://go.to/xi8jwe #goodtimes e) Super dooper. Looks nice! http://22.com/xx / http://p.de

I've played with numerous variations of this regex, but the latest incarnation, which doesn't really work very well, is below:

$string=~s#http://(.*)(\s)#<a href=$1">http://$1</a>$2#g;

As can be seen, I'm trying to create the proper anchor tags to go with the given URL in the string, to create a proper link. The regex above works for very simple examples, but nothing more complex. i.e. it would work on example (d) above, but nothing else.

Any assistance would be much appreciated!


In reply to Regex Query by DanielSpaniel

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.