Regex, getting closer
This line of code successfully notates internal links with <internal>.
What I really want it to do, is to notate the external links.
How do I change the regex to add a notation only a link exists without
"sample.com.au"?
$html_code =~
s{(<a\s+[^>]*?href="([^""]+\.sample.com.au))([^>]*">.*?/a>)}{$1 $3
<internal>}gsi;
Mark
Re: Regex, getting closer
On 10/22/2006 07:23 AM, Mark & Ingrid Nugent wrote:
> This line of code successfully notates internal links with <internal>.
> What I really want it to do, is to notate the external links.
> How do I change the regex to add a notation only a link exists without
> "sample.com.au"?
>
> $html_code =~
> s{(<a\s+[^>]*?href="([^""]+\.sample.com.au))([^>]*">.*?/a>)}{$1 $3
> <internal>}gsi;
>
> Mark
>
>
This code is untested:
sub not_sample {
if ($_[1] =~ /\.sample\.com\.au$/) {
"$_[0] $_[2]";
} else {
"$_[0] $_[2] <not_sample_com_au>";
}
}
$html_code =~
s{(<a\s+[^>]*?href="([^""]+\.sample.com.au))([^>]*">.*?/a>)}
{not_sample($1,$2,$3)}gsei;
Notice that I used the /e option to the s/// operator to tell it to
execute perl code to get the replacement string. That perl code is
not_sample($1,$2,$3), and that subroutine does some simple tests on the
URL before returning the replacement string.
Read the documentation: perldoc perlre
--
paduille.4060.mumia.w [at] earthlink.net