For a concrete example, consider the following snippet of HTML:
If I wished to extract data from this table I might construct a "matcher" using the following (made-up) syntax:<table> <th> <td>Title</td> <td>Subject</td> <td>ISBN</td> <td>Publisher</td> <td>Synopsis</td> </th> <tr> <td>Ender's Game</td> <td>Science Fiction</td> <td>12375856996</td> <td>Macmillan</td> <td>a good book</td></tr> </tr> </table>
This would match <td>Ender's Game</td>, successfully extracting the title of the book. Such a matching scheme is relatively impervious to changes in layout, or even changes in the number of table rows or columns.tagblock <td> following tag <tr> inside { tagblock <table> containing { tagblock <th> { containing regex /Subject/ AND containing regex /ISBN/ AND containing regex /Publisher/ } } }
Consider a second example to find a doubleclick.net advertisement:
tagblock <table width=468 height=60> containing { (tag <img> AND tag <a href =~ /doubleclick.net/>) OR tag <a href =~ /flycast.com/> OR tag <!--/Begin Ad/--> } regex /doubleclick.net/ inside { attrib </img|script|i?frame|i?layer/ src> OR attrib <a href> } inside tag <a|img|script|i?frame|i?layer>
These examples are verbose, and are examples of something that doesn't exist, but I hope the syntax is demonstrative of what I'm trying to do. This could be done using HTML::Parser or HTML::TokeParser and a good bit of work, but it would be very slow (consider the second example: finding doubleclick.net with a regex is much faster than examining every tag with HTML::Parser), and it would be fragile with respect to changes in the HTML document's structure, layout, and content. Of course, once the grammer is written and parsed (Parse::RecDescent), one could implement the matcher using HTML::Parser. By creating a grammar for doing pattern matching on HTML, in a manner that obeys document structure, one could have fast matching with a relatively intuitive matching language. Trying to obey document structure allows one to then modify the matched tidbit without destroying document structure by leaving unclosed tags.
The question is: has this been done? Has anything similar been attempted? Is anyone working on such a thing? Does anyone want to work on such a thing? Do other people besides me have a use for such a thing?
In reply to A grammar for HTML matching by mcelrath
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |