in reply to Key/Value pair from GET

I don't like using long and complex regexes that may break. The URI module is built to parse URIs; why forgo it? On the other hand, all solutions to parse HTML properly require a lot of coding; so to pick out the URLs, we make as few assumptions as possible so that a very simple (and therefor robust) regex will do the trick. The following code only assumes there are links that have a sectionId parameter; we want its value, and we want the link text, minus any tags (whether <span> pairs or something completely different).
#!/usr/bin/perl -w use strict; use URI; # in the realworld, it comes from somewhere else my $page_content = <<'EOT'; <a href="thepage.jsp?siteId=1&sectionId=443&amp;"> <span class="small">Geography</span></a> EOT # the following regex will simply catch any anchor tags, # making no assumptions about their structure while($page_content =~ /<a\s[^>]*?href="([^"]+)"[^>]*>(.*?)<\/a>/sgi) +{ my $url = new URI $1; my %params = $url->query_form; # now we let URI do the dirty job f +or us next unless exists $params{sectionId}; # was there a sectionId par +ameter? # 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 }
____________
Makeshifts last the longest.