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.

In reply to Re: Key/Value pair from GET by Aristotle
in thread Key/Value pair from GET by inblosam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.