Note: Do not use regular expressions to parse HTML.
This first option is done using HTML::TreeBuilder, one of many HTML Parsers that is available to use. You can visit the link provided above and read the documentation and see the example's that are given.
use strict;
use warnings;
use HTML::TreeBuilder;
my $str
= "<ul>"
. "<li>hello</li>"
. "<li>there</li>"
. "<li>everyone</li>"
. "</ul>"
;
# Now create a new tree to parse the HTML from String $str
my $tr = HTML::TreeBuilder->new_from_content($str);
# And now find all <li> tags and create an array with the values.
my @lists =
map { $_->content_list }
$tr->find_by_tag_name('li');
# And loop through the array returning our values.
foreach my $val (@lists) {
print $val, "\n";
}
If you decide you want to use a regular expression here (I don't recommend). You could do something like..
my $str
= "<ul>"
. "<li>hello</li>"
. "<li>there</li>"
. "<li>everyone</li>"
. "</ul>"
;
my @matches;
while ($str =~/(?<=<li>)(.*?)(?=<\/li>)/g) {
push @matches, $1;
}
foreach my $m (@matches) {
print $m, "\n";
}
Output:
hello
there
everyone
How to delete a newline if it is the last character in a file?
How to pass command-line arguments to a Perl program?
How to efficiently calculate a running standard deviation
Howto use a variable in the replacement side of the Perl substitution operator?
How to summ quickly all numbers in a file?
How to remove duplicate items from an array in Perl?
How to differ of Two Arrays Using Perl