How do I find this div using regex in Wordpress/Autoblogged?

252 views Asked by At

How do you find the following div using regex? The URL and image location will consistently change based on the post URL, so I need to use a wild card.

I must use a regular expression because I am limited in what I can use due to the software I am using: http://community.autoblogged.com/entries/344640-common-search-and-replace-patterns

<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjumpinblack.com%2F2011%2F11%2F25%2Fdrake-and-rick-ross-you-only-live-once-ep-mixtape-2011-download%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjumpinblack.com%2F2011%2F11%2F25%2Fdrake-and-rick-ross-you-only-live-once-ep-mixtape-2011-download%2F&amp;source=jumpinblack1&amp;style=compact&amp;b=2" height="61" width="50" /><br /> </a> </div>

I tried using

<div class="tweetmeme_button" style="float: right; margin-left: 10px;">.*<\/div>
2

There are 2 answers

1
gangabass On BEST ANSWER

Using regular expression to process HTML is a bad idea. I'm using HTML::TreeBuilder::XPath for this.

use strict;
use warnings;
use HTML::TreeBuilder::XPath;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->get("http://www.someURL.com");

my $tree = HTML::TreeBuilder::XPath->new_from_content( $mech->content() );    
my $div = $tree->findnodes( '//div[@class="tweetmeme_button"]')->[0];
0
Sinan Ünür On

Use an HTML parser to parse HTML.

HTML::TokeParser::Simple or HTML::TreeBuilder::XPath among many others.

E.g.:

#!/usr/bin/env perl

use strict;
use warnings;

use HTML::TokeParser::Simple;

my $parser = HTML::TokeParser::Simple->new( ... );

while (my $div = $parser->get_tag) {
    next unless $div->is_start_tag('div');
    {
        no warnings 'uninitialized';
        next unless $div->get_attr('class') eq 'tweetmeme_button';
        next unless $div->get_attr('style') eq 'float: right; margin-left: 10px;'
        # now do what you want until the next </div>
    }

}