
miller
User
May 4, 2011, 10:00 AM
Post #3 of 3
(1598 views)
|
|
Re: [techtween] How to count same HTML tags
[In reply to]
|
Can't Post
|
|
Don't use a regex, use an html parser like HTML::TreeBuilder
use HTML::TreeBuilder; use strict; use warnings; my $data = do {local $/; <DATA>}; my $tree = HTML::TreeBuilder->new; $tree->parse($data); my $tag = 'b'; my @tags = $tree->look_down('_tag' => $tag); print "$tag has: " . scalar(@tags) . "\n"; __DATA__ <html> <body> <b>file1</b><b>file2</b> </body> </html> If you insist on a regex, than something like the following would work
use strict; use warnings; my $data = do {local $/; <DATA>}; my $tag = 'b'; my $count = () = $data =~ /<\Q$tag\E(?:\s.*?)?>/g; print "$tag has: " . $count . "\n"; __DATA__ <html> <body> <b>file1</b><b>file2</b> </body> </html>
|