4 ms·
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 clearl
by albertzeyer 16y ago
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?
- vog 16y agoThat'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.
- jauco 16y agoFor 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>
- albertzeyer 16y agoAll kind of ways you could write the br-tag are regular.
- jauco 16y agoNo, because as the example shows I can put regular html in the id tag. Once you do nesting, it's not regular anymore.
- albertzeyer 16y agoYou 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.
- megaduck 16y agoThe 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.
- albertzeyer 16y agoBut 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).