• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Parsing HTML With Regular Expressions

    February 22nd, 2013
    html, tech  [html]
    Perhaps you need to get some information out of HTML. Regular expressions look promising, but you get stuck so you ask for help. A typical response would be:
    HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag. Regular expressions can only match regular languages but HTML is a context-free language.
    This is true, but only in a sense so narrow it's useless. When someone asks about "regular expressions" they don't mean the restricted computer science kind but the implementations available in various programming languages. Those are much more powerful, capable of parsing HTML.

    Here again we're using dangerously precise words. When someone says "HTML" they don't mean HTML as defined by the spec but HTML that they are likely to encounter in the real world. Which means you might need to handle monstrosities like "<i>italic <b>bold-italic</i> bold</b>" which have no parse tree but your browser still renders as italic bold-italic bold. [1]

    But again with the precision! For typical tasks you don't need to 'parse' HTML, just extract some information from it. Regular expressions and other ad-hoc techniques can do that well and are likely to be less trouble in practice [2] than trying to use a parser.

    (Because mod_pagespeed needs to make context-dependent changes to arbitrary web pages, regular expressions are not a good fit. You might think using the parsing code from a web browser would work well, but it turns out that browsers mix HTML-parsing with HTML-cleanup [1]. So pagespeed instead goes token-by-token, triggering callbacks for start and end tags.)


    [1] Webkit turns <i>italic <b>bold-italic</i> bold</b> into <i>italic <b>bold-italic</b></i> <b>bold<b> at an early stage of interpreting the page.

    [2] The advice to use an XML parser is just bad, as funny as the author is. XML parsers get to assume they're only going to be given valid XML and just reject anything that isn't. Extremely few pages are (or even try to be) XML, so your XML parser isn't going to be helpful.

    Comment via: google plus, facebook, hacker news

    Recent posts on blogs I like:

    Collections: The Queen’s Latin or Who Were the Romans, Part V: Saving And Losing an Empire

    This is the fifth and final part (I, II, III, IV) of our series asking the question ‘Who were the Romans?’ How did they understand themselves as a people and the idea of ‘Roman’ as an identity? Was this a homogeneous, ethnically defined group, as some ver…

    via A Collection of Unmitigated Pedantry July 30, 2021

    Modernizing Rail 2021 Announcement

    We are happy to announce that on Sunday the 29th of August we will hold this year’s Modernizing Rail conference, on the heels of the success last year. Please register using this form. And please give details on what you’d like to see, and if you’re willi…

    via Pedestrian Observations July 29, 2021

    Songs about terrible relationships

    [Spoilers for several old musicals.] TV Tropes lists dozens of examples of the “I want” song (where the hero of a musical sings about their dream of escaping their small surroundings). After watching a bunch of musicals on maternity leave, I’m wondering h…

    via The whole sky July 17, 2021

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact