For the curious, I'll post what I ended up doing. One of the main requirements of this script was that it carefully preserved the formatting of the files that it ran on- the only change could be the addition of the url markers, so that influenced my solution. the script would be called with two files- one html and one plain text. Sample html file to call with:
this a link: <br> <a href="http://www.somewhere.org/foo">somewhere</a> +<p> <a href= https://somewhere.else.net/bar/>another place to go</a>. +<p>
And sample text file pair to the html file:
this a link: http://www.somewhere.org/foo another place to go: https://somewhere.else.net/bar/
so here's my code to tag the urls in these files:
#$unprocessed_html holds the text of the html version of my file #$unprocessed_text holds the plain text version of my file #@markers holds the unique url markers, ie [/qfk33pe][/nnd92093] #handle html version my $processed_html = ""; while (length($unprocessed_html) > 0) { if($unprocessed_html =~ s/^(.*?\b(href|action)\s*=\s*)//si) { $processed_html .= $1; } else { $processed_html .= $unprocessed_html; last; } if (not(@markers)){ $processed_html .= $unprocessed_html; warn "no more markers available for remaining links\n"; last; } if($unprocessed_html =~ s/^([^\"\'][^<>\s]*)//) { my $url = $1 . shift(@markers); #strip double //s due to sloppy input $url =~ s|//|/|g; $processed_html .= $url; } elsif($unprocessed_html =~ s/^([\"\'])([^<>]*?)\1//) { my $url = $1 . $2 . shift(@markers) . $1; #strip double //s that can result from sloppy input $url =~ s|//|/|g; $processed_html .= $url; } else { die "something happened here"; } } #handle text version my $processed_text = ""; while (length($unprocessed_text) > 0) { if($unprocessed_text =~ s/^(.*?\b)http/http/si) { $processed_text .= $1; } else { $processed_text .= $unprocessed_text; last; } if (not(@markers)){ $processed_text .= $unprocessed_text; warn "no more markers available for remaining links\n"; last; } if($unprocessed_text =~ s/(http[\S]+)\s+//){ my $urlfound = $1 . shift (@markers); $urlfound =~ s|//|/|g; $processed_text .= "$urlfound\n"; } elsif($unprocessed_text =~ s/(http[\S]+)$//){ my $urlfound = $1 . shift (@markers); $urlfound =~ s|//|/|g; $processed_text .= "$urlfound\n"; } }
-- cat

In reply to My end solution to appending a unique marker to each url in a file by cat2014
in thread appending a unique marker to each url in a file by cat2014

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.