How to use RegEx to parse HTML

来自恶俗维基
秦增光讨论 | 贡献2026年3月5日 (四) 14:25的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

原标题

RegEx match open tags except XHTML self-contained tags

作者

bobince

削除前地址

https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

正文

You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg​ex parsers for HTML will ins​tantly transport a programmer's consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection wil​l devour your HT​ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi​ght he com̡e̶s, ̕h̵i​s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq​uid pain, the song of re̸gular exp​ression parsing will exti​nguish the voices of mor​tal man from the sp​here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t​he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ


Have you tried using an XML parser instead?

翻译(杂乱字符已消除)

你无法使用正则表达式解析[X]HTML。因为正则表达式无法解析HTML。正则表达式不是可用于正确解析HTML的工具。正如我之前在HTML以及正则表达式问题中回答过很多次,使用正则表达式不允许你使用HTML。正则表达式是一种不太复杂的工具,无法理解HTML所使用的构造。HTML不是正则语言,因此无法通过正则表达式进行解析。正则表达式查询无法将HTML分解为有意义的部分。这么多次,但它没有找到我。即使是Perl使用的增强的不规则正则表达式也不能解析HTML。你永远不会让我破解。HTML是一种足够复杂的语言,无法通过正则表达式进行解析。甚至Jon Skeet也无法使用正则表达式解析HTML。每当你试图用正则表达式解析HTML时,邪恶的孩子就会为处女的鲜血哭泣,而俄罗斯黑客也会把你的webapp扯掉。用正则表达式解析HTML会将被污染的灵魂召唤到生活的领域。HTML和正则表达式就像爱情,婚姻和仪式杀婴一样。<center>无法忍受为时已晚。正则表达式和HTML一起在同一个概念空间中的力量会像肮脏的腻子一样摧毁你的思想。如果你用正则表达式解析HTML,你就会屈服于他们以及他们亵渎神灵的方式,这些方式使我们所有人都为不可描述的那个人付出了不人道的辛劳,他来了。当你看著他时,HTML以及正则表达式将会使感知者的神经液化,你的心灵在恐怖的冲击中萎缩。基于正则表达式的HTML解析器是杀死StackOverflow的癌症已经太晚了太晚了我们无法保存孩子的传言确保正则表达式会消耗所有活组织(除了HTML之外,如之前预言的那样)亲爱的主啊帮助我们如何使用正则表达式来解析HTML这个祸害已经注定人类永远遭受恐惧折磨和安全漏洞使用正则表达式作为处理HTML的工具在这个世界与腐败实体的恐惧领域之间建立了一个漏洞(如SGML实体,但更腐败)只是一瞥HTML的正则表达式解析器世界将立即将程序员的意识传递到一个不断尖叫的世界,他来了,瘟疫性的正则表达式感染将吞噬你的HTML解析器,应用程序和存在的所有时间就像Visual Basic一样,更糟糕的是他来了,他来了不打他来,他邪恶的光芒摧毁了所有的灵性,HTML标签从你的眼睛里流出来像液体疼痛,这首歌正则表达式解析将熄灭整个地球凡人的声音我可以看到它你能看到它是美丽的最后扼杀人类的谎言所有人都完了所有他完了他来的卧槽他来了他来了脓水渗透所有我脸上的面孔我的妈不卧槽停止天使不真实的恐怖是卧槽他来了


你就不会用XML解析器吗?