News for hackers. Stuff that matters.
Re:Stackoverflow, HTML by Regex, topmost answer by albertzeyer on Monday July 05, 2010 @08:51AM (#1488092)
Ehm, I wonder a bit, the discussions always goes that HTML is not regular. The poster though asked to just match any open tags. The language of HTML tags clearly is regular, isn't it?
Re:Stackoverflow, HTML by Regex, topmost answer by jauco on Monday July 05, 2010 @11:42AM (#1488435)
For one thing: <br> is valid html, and this too:

    <!DOCTYPE html>
    <html>
    <head>
       <title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
    </head>
    <body>
       <div>
    <br id="<bl>">
       </div>
    </body>
    </html>
Re:Stackoverflow, HTML by Regex, topmost answer by albertzeyer on Monday July 05, 2010 @12:40PM (#1488562)
All kind of ways you could write the br-tag are regular.
Re:Stackoverflow, HTML by Regex, topmost answer by jauco on Monday July 05, 2010 @01:49PM (#1488699)
No, because as the example shows I can put regular html in the id tag. Once you do nesting, it's not regular anymore.
Re:Stackoverflow, HTML by Regex, topmost answer by albertzeyer on Monday July 05, 2010 @03:20PM (#1488886)
You cannot nest the quotes. I.e. this is invalid:

    <br id="<br id="<br>">">
I.e. it doesn't matter what is inside the quotes.
Re:Stackoverflow, HTML by Regex, topmost answer by megaduck on Monday July 05, 2010 @12:11PM (#1488512)
The language of individual HTML tags is certainly regular, and trivially easy. However, the language of "matched HTML tags with junk between them" is NOT regular.

Anything that requires balanced matching is NOT parseable with standard Regular Expressions, and by not parseable I mean that you will literally have an infinite amount of bugs. Shoot me an email and I can show you the math.

Even with Perl's whiz-bang recursive not-really-regexes-regexes, it's strongly not recommended to tackle balanced matching problems like HTML or XML. It might be theoretically possible (I haven't actually checked), but your brain will leak from your ears and you probably won't get it right, no matter how smart you are.

Re:Stackoverflow, HTML by Regex, topmost answer by albertzeyer on Monday July 05, 2010 @12:39PM (#1488561)
But the OP asked for parsing just for a specific type of tag (which is regular). Not an area of text between open/close tags (which is not regular).
Re:Stackoverflow, HTML by Regex, topmost answer by vog on Monday July 05, 2010 @09:50AM (#1488185)
That's what I'm wondering, too.

If it was XML, one might get in trouble with "<[CDATA[" sections, but regarding HTML, I don't see a real issue here.

... especially not from the pragmatic point of view. Depending on the use case and the quality of the HTML source, a "dirty" regex hack might be a far better solution and using a DOM parser.