Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6195101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:24:38+00:00 2026-05-24T03:24:38+00:00

There is no day on SO that passes without a question about parsing (X)HTML

  • 0

There is no day on SO that passes without a question about parsing (X)HTML or XML with regular expressions being asked.

While it’s relatively easy to come up with examples that demonstrates the non-viability of regexes for this task or with a collection of expressions to represent the concept, I could still not find on SO a formal explanation of why this is not possible done in layman’s terms.

The only formal explanations I could find so far on this site are probably extremely accurate, but also quite cryptic to the self-taught programmer:

the flaw here is that HTML is a Chomsky Type 2 grammar (context free
grammar) and RegEx is a Chomsky Type 3 grammar (regular expression)

or:

Regular expressions can only match regular languages but HTML is a
context-free language.

or:

A finite automaton (which is the data structure underlying a regular
expression) does not have memory apart from the state it’s in, and if
you have arbitrarily deep nesting, you need an arbitrarily large
automaton, which collides with the notion of a finite automaton.

or:

The Pumping lemma for regular languages is the reason why you can’t do
that.

[The majority of the above explanation link to wikipedia pages, but these are not much easier to understand than the answers themselves].

What is a translation in layman’s terms of the formal explanations given above of why it is not possible to use regex for parsing (X)HTML/XML?

I am looking for a translation that also briefly explains the concepts it tries to translate: at the end of an answer, the reader should have a rough idea –for example–of what "regular language" and "context-free grammar" mean.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:24:39+00:00Added an answer on May 24, 2026 at 3:24 am

    Concentrate on this one:

    A finite automaton (which is the data structure underlying a regular
    expression) does not have memory apart from the state it’s in, and if
    you have arbitrarily deep nesting, you need an arbitrarily large
    automaton, which collides with the notion of a finite automaton.

    The definition of regular expressions is equivalent to the fact that a test of whether a string matches the pattern can be performed by a finite automaton (one different automaton for each pattern). A finite automaton has no memory – no stack, no heap, no infinite tape to scribble on. All it has is a finite number of internal states, each of which can read a unit of input from the string being tested, and use that to decide which state to move to next. As special cases, it has two termination states: “yes, that matched”, and “no, that didn’t match”.

    HTML, on the other hand, has structures that can nest arbitrarily deep. To determine whether a file is valid HTML or not, you need to check that all the closing tags match a previous opening tag. To understand it, you need to know which element is being closed. Without any means to “remember” what opening tags you’ve seen, no chance.

    Note however that most “regex” libraries actually permit more than just the strict definition of regular expressions. If they can match back-references, then they’ve gone beyond a regular language. So the reason why you shouldn’t use a regex library on HTML is a little more complex than the simple fact that HTML is not regular.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some background: I came across something the other day that got me thinking about
I was asked this question while interviewing for a startup and saw this again
There is nothing like the 43rd day of your life spent tracking down issues
There is some type Record : type Day = Integer type Description = String
Is there any way to show only week day with Date as: MON 03-JUL
Good day! There are a lot of questions how to store tags in DB
Is there a way to figure out the first day of the month (min
Is there any formula to direct finding D.O.B from put Year,month and day's in
I understand there is a limit to the number of requests per day using
Anticipating the day when multi-touch interfaces become more pervasive, are there libraries in Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.