#!/usr/bin/perl -w
use strict;
use URI;
# in the realworld, it comes from somewhere else
my $page_content = <<'EOT';
Geography
EOT
# the following regex will simply catch any anchor tags,
# making no assumptions about their structure
while($page_content =~ /]*?href="([^"]+)"[^>]*>(.*?)<\/a>/sgi) {
my $url = new URI $1;
my %params = $url->query_form; # now we let URI do the dirty job for us
next unless exists $params{sectionId}; # was there a sectionId parameter?
# if we're still in the loop here, there was
my $text = $2;
$text =~ s/<[^>]*>//sgi; # strip all tags from the anchor's inner text
$text =~ s/^\s+//sgi; # then strip any whitespace from the front
$text =~ s/\s+$//sgi; # and from the end
print "$params{sectionId},$text\n"; # some pretty output
}