Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to grab a portion of file with regex

by kielstirling (Scribe)
on Mar 15, 2013 at 00:01 UTC ( #1023583=note: print w/ replies, xml ) Need Help??


in reply to How to grab a portion of file with regex

Hi,

It is generally not recommended to use regex matches to parse HTML files.

Instead as swkronenfeld pointed out its better to use the CPAN module HTML::Parser

Below is an example of its usage.
#!/usr/bin/perl use Modern::Perl; use autodie; use HTML::Parser (); my $p = HTML::Parser->new( start_h => [\&start, 'tagname, attr'], ); open my $fh, '<', shift; $p->parse_file($fh); $fh->close; sub start { my ($tag_name, $attrs) = @_; return unless $tag_name eq 'div'; say 'sample Text' if exists $attrs->{class} and $attrs->{class} and $attrs->{class} =~ /^lastUnit. +*/; }
-Kiel


Comment on Re: How to grab a portion of file with regex
Download Code
Re^2: How to grab a portion of file with regex
by Anonymous Monk on Mar 15, 2013 at 01:46 UTC

    Instead as swkronenfeld pointed out its better to use the CPAN module HTML::Parser

    Not by much, HTML::Parser is very low-level, use a DOM parser supporting xpaths

      Well instead of trolling why not supply a working example to help ??

      Its always the Anonymous Monk lacking courage to put a name to a comment

        Well instead of trolling why not supply a working example to help ?? Its always the Anonymous Monk lacking courage to put a name to a comment

        How is it trolling to point out the shortcomings of a "solution"? Maybe you should look up the definition of troll

        What courage is required to point out a simple fact about HTML::Parser? Are you under the impression that HTML::Parser is a high level parser?

        Your "solution" doesn't fetch the portion of page from class = lastUnit to class = line margin10 -- its incomplete -- it is lots easier/shorter/simpler to use  m{\Q$start\E(.+?)\Q$end\E}i instead of that HTML::Parser low-levelness

        Have you seen Re: How to grab a portion of file with regex (don't)? Its not unlike a minimum of three different tutorials/walkthroughs/step-by-step-instructions on extracting/xpathing the dom , some even compare/contrast with HTML::Parser

      And for html files that are 9,000 GB's in size?
        Always limits to everything. I must remind you that I am not the one wanting to parse HTML. I am simply trying to offer guidance. I understand that HTML parsing is a hot topic. However, as a solution to the question asked HTML::Parser works fine.

        And for html files that are 9,000 GB's in size?

        Nevermind that that 9k-GB html-files don't exit, you can still use XML::Twig, naturally

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023583]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-07-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (174 votes), past polls