CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
How to count same HTML tags

 



techtween
Novice

May 4, 2011, 4:37 AM

Post #1 of 3 (2829 views)
How to count same HTML tags Can't Post

Kindly help me with the regular expression to count same html tags.

example,
<html>
<body>
<b>file1</b><b>file2</b>
</body>
</html>
output => two (since two <b> tags are present in the file)

I need the expression to match any such same tags not specific to one tag.


rovf
Veteran

May 4, 2011, 8:44 AM

Post #2 of 3 (2811 views)
Re: [techtween] How to count same HTML tags [In reply to] Can't Post

Evaluate


Code
$html_text =~ /<b>/g


in scalar context.


miller
User

May 4, 2011, 10:00 AM

Post #3 of 3 (2809 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


Code
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


Code
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>


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives