For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > October 2006 > Regex, getting closer









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regex, getting closer
Mark & Ingrid Nugent

2006-10-22, 7:57 am

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


Mumia W. (reading news)

2006-10-23, 7:58 am

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@earthlink.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com