inblosam has asked for the wisdom of the Perl Monks concerning the following question:

The following code gives me ALMOST what I want. I need the ID and then the name (between the > and <). I am not that familiar with regex, and got some help on this code here too. I am looking at the HTML::TokeParser right now, but it doesn't seem to be the ideal module for doing what I am doing. Any other module suggestions would be appreciated, but I also want to try and figure out how I can do this in regex.

Here is an example of the text I am combing through from the page:
<td colspan="6" bgcolor="#FFFFCC" class="SmallPrintBold"><a href="Plat +formProductDetail.jsp?siteId=1&jid=3X223CA2D129EF993166683A761A7X4D&z +sortParams=true&productId=26636&productType=2&catalog=1&authorId=9364 +0&sectionId=0&platformId=1" >inMeeting</a> </td>

And here is my perl:
use LWP::Simple; my %categories; my $page = get('http://www.handango.com/PlatformTopSoftware.jsp?author +Id=93640&zsortParams=true'); while ($page =~ / productId=(\d+) ([^<]*) < /xig) { $categories{$1} = $2; print "[$1] and [$2]\n"; } foreach my $idkey (keys %categories) { print "$idkey,$categories{$idkey}\n"; }

This is what I get with this code:
[25297] and [&productType=2&catalog=1&authorId=93640&sectionId=0&platf +ormId=1" >inChemLab] [25297] and [">] [26636] and [&productType=2&catalog=1&authorId=93640&sectionId=0&platf +ormId=1" >inMeeting] [26636] and [">] [26632] and [&productType=2&catalog=1&authorId=93640&sectionId=0&platf +ormId=1" >inTimeCard] [26632] and [">] [26641] and [&productType=2&catalog=1&authorId=93640&sectionId=0&platf +ormId=1" >inShop] [26641] and [">] 26636,"> 26632,"> 26641,"> 25297,">

I would like to get just the ID and name, like:
26636,inMeeting 26641,inShop


Michael Jensen
michael at inshift.com
http://www.inshift.com

Replies are listed 'Best First'.
Re: Regex refining...
by Joost (Canon) on Jun 11, 2002 at 15:47 UTC
    This
    $page =~ / productId=(\d+) [^>]* > ([^<]+) < /xig)
    fixes the problem. -- Joost downtime n. The period during which a system is error-free and immune from user input. </code>
      Worked perfect! I knew I was close, so thanks for that fix!

      Michael Jensen
      michael at inshift.com
      http://www.inshift.com
Re: Regex refining...
by neilwatson (Priest) on Jun 11, 2002 at 16:11 UTC
    I would do it like this:
    m/productId=(\d+)/ && m/<\s*(\w+)\s*<\/a>/

    So that $1 is your productId and $2 is the string between >inMeeting</a>

    Neil Watson
    watson-wilson.ca