I think it is worth adding to this thread a quick explanation on why regular expressions are a very risky way to process HTML (as well as XML).

<many excellent examples SNIPPed>

Al the above is absolutely true for arbitrary webpages - as in the problem you repsonded to.

However one of the more common LWP etc. tasks is to retrieve and search for/extract information from the web front ends of query tools (such as Pubmed or Scirus (or google although google doesn't like LWP)) or other template built webpages (such as this one). This is where the recent micro-optimisation comments apply. In such a case regexes or even simpler index($html, $string) statements are much faster and consume fewer resources. The difference can mean that your script can run OK on an ancient PC versus requiring oodles of processing power/memory.

Moreover for that kind of operation navigating the complex hashes and hashes of arrays of hashes etc. produced by HTML::TokeParser::* and relatives is probably harder to maintain in the event of layout changes than the regex method. Consider what happens when a TABLE is replaced by a set of positioned DIVs and ULs for example, but all you want is to see if "Fred Bloggs" appears anywhere in the results section.

Summary: If you want to get a small amount of information from a particular tag or tags from arbitrary HTML then TokeParser is definitely the way to go. On the other hand if you want to extract large amounts of content from a series of similar pages then regexes are not a bad way to do things.

Dingus


Enter any 47-digit prime number to continue.

In reply to Re: Re: Fetching meta tag info by dingus
in thread Fetching meta tag info by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.