Разбор HTML с помощью регулярных выражений - ужасная идея. Мое текущее оружие выбора при разборе HTML - Web :: Query .
Мой подход заключается в том, чтобы разобрать таблицу в подходящую структуру данных, которая затем позволяет вам извлекать нужные данные.
Что-то вроде этого, возможно ...
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
use Path::Tiny;
use Web::Query;
# Get the HTML. I'm reading it from a file, you might
# need to make an HTTP request.
my $html = path('table.html')->slurp;
# Parse the HTML.
my $wq = wq($html);
# Extract the text from all of the <th> elements.
# These will be the keys of our hash.
my @cols;
$wq->find('th')->each(sub { push @cols, $_->text });
# A hash to store our data
my %data;
# Find all of the <tr> elements in the HTML
$wq->find('tr')->each(sub {
# Each table row will be a sub-hash in our hash
my %rec;
# Find each <td> element in the row.
# For each element, get the text and match it with the column header.
# Store the key/value pair in a hash.
$_->find('td')->each(sub {
my ($i, $elem) = @_;
my $key = $cols[$i];
$rec{$key} = $elem->text;
});
# If we have data, then store it in the main hash.
$data{$rec{Name}} = \%rec if $rec{Name};
});
# Show what we've made.
say Dumper \%data;
# A sample query which extracts all of the values
# from the data structure.
for (keys %data) {
say "$_ is $data{$_}{Values}";
}