You can't parse [X]HTML with regex

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • lexbor

    Lexbor is development of an open source HTML Renderer library. https://lexbor.com

  • I think we've all (mostly?) tried it. It really is the Wild West of the web when you're trying to parse other people's HTML, though.

    I've played around with this parser which is extremely quick. https://github.com/lexbor/lexbor

  • oil

    Oils is our upgrade path from bash to a better language and runtime. It's also for Python and JavaScript users who avoid shell!

  • All you need to parse HTML is regular expressions (to recognize tags) and a stack (to match tags).

    Your programming language has a stack -- a call stack.

    So in practice all you really need is regular expressions. (Which I tend to call regular languages to make a distinction with Perl-style regexes [1])

    Using the call stack in a more functional style is nicer than using the OOP style that s in the Python standard library, which is probably inherited from Java, etc.

    I have done this with a bunch of HTML processors for the Oil blog and doc toolchain:

    https://github.com/oilshell/oil/tree/master/doctools

    It works well in practice and is correct and fast. I meant to write a blog post titled "why you can parse HTML with regexes" about this but didn't get around to it.

    There is a nit where parsing arbitrary name=value pairs with regexes isn't ergonomic, because it's hard to capture a variable number of pairs. However the point is that I wrote 5 or 6 useful and compact HTML processors that don't need that. In practice when you parse HTML you often a "fixed" schema.

    Concrete examples are generating a TOC from

    ,

    , etc. and syntax highlighting
     blocks.  Those all work great with the regex + call stack style.

    [1] http://www.oilshell.org/blog/2020/07/eggex-theory.html

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • CPython

    The Python programming language

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts