I am not an expert in Perl but I have written a Perl script to parse an HTML page and filter by all href tags:
The output are as shown below:
href="?Name">Name</a>
href="?Desc">Hourly Details</a>
href="/24x7/2012/11-November/">Data
href="./00:00:00/">00:00:00/</a>
href="./01:00:00/">01:00:00/</a>
href="./02:00:00/">02:00:00/</a>
href="./03:00:00/">03:00:00/</a>
href="./04:00:00/">04:00:00/</a>
href="./05:00:00/">05:00:00/</a>
href="./06:00:00/">06:00:00/</a>
href="./07:00:00/">07:00:00/</a>
href="./08:00:00/">08:00:00/</a>
href="./09:00:00/">09:00:00/</a>
href="./10:00:00/">10:00:00/</a>
href="./11:00:00/">11:00:00/</a>
href="./12:00:00/">12:00:00/</a>
href="./13:00:00/">13:00:00/</a>
href="./14:00:00/">14:00:00/</a>
href="./15:00:00/">15:00:00/</a>
href="./16:00:00/">16:00:00/</a>
href="./17:00:00/">17:00:00/</a>
href="./18:00:00/">18:00:00/</a>
href="./19:00:00/">19:00:00/</a>
href="./20:00:00/">20:00:00/</a>
href="./21:00:00/">21:00:00/</a>
href="./22:00:00/">22:00:00/</a>
href="./23:00:00/">23:00:00/</a>
Now I want to extract values within the href tags from “00:00:00” till “23:00:00” while exclude others. The result value would be added to string having a URL:
http://x.download.com/00:00:00
------URL------------/..href../
..............................
http://x.download.com/23:00:00
However by trying the below code:
foreach (@tag) {
if (m/href/) {
if ($_ =~ /"\/24/ && $_ =~ /"\/[0-9]/) {
my $href = $_;
my $start = index($href, "\"");
my $end = rindex($href, "\"");
my $link = substr($href, $start + 1, $end - $start - 1);
print "Follow: " . $url . $link . "\n";
}
}
}
prints:
Follow: http://x.download.com/24x7/2012/11-November/
What should my regular expression be such that required objective can be achieved?
This is done very simply with a regular expression, as shown in the program below. It looks for a string of digits or colons immediately following
>(and so looks for the text contents of the element rather thehrefattribute value as yours does) and captures that string into$1.But I would prefer to see the problem solved from start to finish using a proper HTML parser, such as
HTML::TreeBuilderor
Mojo::DOM.output