in reply to Re: multiple regex matching
in thread multiple regex matching

Let me retry to explain what I need to do.

I am scraping a web page and I have $source which is the html dump. From the entire source code there is a table that has a ton of rows but always 2 columns (as you can see from the two samples above.

There is 1 URL in each table cell which means 2 URLS per table row. I need to collect the URL and the text of the URL for EACH table cell (which means 2 urls per row).

Those 2 samples displayed in the OP is what the table looks like. There's dozens upon dozens of <TR>s and I need to extract each url and the text for it and at the same time keep each row of links connected. This is to say I need to keep the two URLS per row together somehow because they are relative to each other. This code is the beginning of the table and 3 rows. You see it's just the same type of code I pasted earlier. I need the urls and the text but keep each row of data together magically.

<TABLE width=468 style="border-collapse:collapse"><TR><TD><TABLE width +=468 style="border-collapse:collapse" cellpadding=2> <TR><TD class=he +ad>Artist</TD><TD class=head>Song</TD></TR> <TR class=g><TD class=i><A href="/my-bloody-valentine-rxzg3.html">My B +loody Valentine</A></TD><TD class=j><A href="/my-bloody-valentine-lov +ely-sweet-darlene-9lgrnkb.html">Lovely Sweet Darlene *</A></TD></TR> <TR class=h><TD class=i><A href="/jennifer-love-hewitt-d193f.html">Jen +nifer Love Hewitt</A></TD><TD class=j><A href="/jennifer-love-hewitt- +i-want-a-love-i-can-see-jv2mj67.html">I Want A Love I Can See *</A></ +TD></TR> <TR class=g><TD class=i><A href="/jennifer-love-hewitt-d193f.html">Jen +nifer Love Hewitt</A></TD><TD class=j><A href="/jennifer-love-hewitt- +no-ordinary-love-4rftntr.html">No Ordinary Love *</A></TD></TR>
From that code I want to get /my-bloody-valentine-rxzg3.html and My Bloody Valentine from the first cell from the first table and ="/my-bloody-valentine-rxzg3.html and Lovely Sweet Darlene . I need this data collected for each row.

Does this make any more sense?

Edited by Chady -- escaped <TR>

Replies are listed 'Best First'.
Re^3: multiple regex matching
by Cody Pendant (Prior) on Apr 14, 2005 at 01:22 UTC
    Well, this does it, but it's not the optimal solution. There's lots of repetition and one side is clearly the artist and the other a song, so it looks kind of off to me. You know better than me what you want though...

    I never thought I'd see MBF and JLH mentioned in the same breath...

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; my $html = '<TABLE width=468 style="border-collapse:collapse"> <TR><TD><TABLE width=468 style="border-collapse:collapse" cellpadding= +2> <TR><TD class=head>Artist</TD><TD class=head>Song</TD></TR> <TR class=g><TD class=i><A href="/my-bloody-valentine-rxzg3.html">My B +loody Valentine</A></TD><TD class=j><A href="/my-bloody-valentine-lov +ely-sweet-darlene-9lgrnkb.html">Lovely Sweet Darlene *</A></TD></TR> <TR class=h><TD class=i><A href="/jennifer-love-hewitt-d193f.html">Jen +nifer Love Hewitt</A></TD><TD class=j><A href="/jennifer-love-hewitt- +i-want-a-love-i-can-see-jv2mj67.html">I Want A Love I Can See *</A></ +TD></TR> <TR class=g><TD class=i><A href="/jennifer-love-hewitt-d193f.html">Jen +nifer Love Hewitt</A></TD><TD class=j><A href="/jennifer-love-hewitt- +no-ordinary-love-4rftntr.html">No Ordinary Love *</A></TD></TR>'; my @data = (); while ( $html =~ m|<TD class=\w><A href="([^"]+)">([^<]+)</A></TD>|g ) { push( @data, { link => $1, name => $2 } ); } print Dumper(\@data); #### result #### # $VAR1 = [ # { # 'link' => '/my-bloody-valentine-rxzg3.html', # 'name' => 'My Bloody Valentine' # }, # { # 'link' => '/my-bloody-valentine-lovely-sweet-darlene-9lgrnkb.ht +ml', # 'name' => 'Lovely Sweet Darlene *' # }, # { # 'link' => '/jennifer-love-hewitt-d193f.html', # 'name' => 'Jennifer Love Hewitt' # }, # { # 'link' => '/jennifer-love-hewitt-i-want-a-love-i-can-see-jv2mj6 +7.html', # 'name' => 'I Want A Love I Can See *' # }, # { # 'link' => '/jennifer-love-hewitt-d193f.html', # 'name' => 'Jennifer Love Hewitt' # }, # { # 'link' => '/jennifer-love-hewitt-no-ordinary-love-4rftntr.html' +, # 'name' => 'No Ordinary Love *' # } # ]; #


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print