arturo is right, the problem is too complex to be handled just using regexp's (what about the base element?)

Nonetheless I'll give it a go, as it demonstrate one of my favourite techniques... I just grab the href attribute in the first regexp and then use a subroutine to analyze it and modify it appropriately. I think this should work for simple links:

#!/bin/perl -w use strict; my $server = "http://www.foo.com"; my $path = "/absolute/path/"; while( <DATA>) { s{<\s*a\s*href=(['"])(.*?)\1\s*>([^<]*)</a>} { build_link( $2, $3) }seg; # hand ou +t the updating to a subroutine print; } sub build_link # here we + can play with the href attribute { my( $href, $text)= @_; if( $href=~ m{^(http|telnet|gopher|file|wais|ftp)://}) { return qq{<a href="$href">$text</a>}; } elsif( $href=~ m{^/}) { return qq{<a href="$server$href">$text</a>}; } else { return qq{<a href="$server$path$href">$text</a>}; } } __DATA__ <a href="/absolute/no/dns">absolute with no dns</a> <a href="http://absolute.with/dns.html">http://absolute.with/dns.html< +/a> <a href="relative/without/dns.html">relative/without/dns.html</a> <a href="relative2/without/dns.html">relative2 without dns.html</a> <a href="relative/without/dns.html">relative/without/dns.html</a> <a name="toto">toto</a>

In reply to Re: matching the non-presence of a string by mirod
in thread matching the non-presence of a string by Joey The Saint

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.