似乎在stackoverflow上的每个问题中,提问者使用regex从HTML中获取一些信息,将不可避免地有一个“答案”,说不要使用regex解析HTML。

为什么不呢?我知道有一些所谓的“真正的”HTML解析器,比如Beautiful Soup,我相信它们是强大而有用的,但如果您只是在做一些简单、快速或简单的事情,那么当一些正则表达式语句就可以很好地工作时,为什么要麻烦使用如此复杂的东西呢?

此外,是否只是因为我不理解正则表达式的某些基本原理,才使得它们在解析中成为一个糟糕的选择?


当前回答

You, know...there's a lot of mentality of you CAN'T do it and I think that everyone on both sides of the fence are right and wrong. You CAN do it, but it takes a little more processing than just running one regex against it. Take this (I wrote this inside of an hour) as an example. It assumes the HTML is completely valid, but depending on what language you're using to apply the aforementioned regex, you could do some fixing of the HTML to make sure that it will succeed. For example, removing closing tags that are not supposed to be there: </img> for example. Then, add the closing single HTML forward slash to elements that are missing them, etc.

我将在编写一个库的上下文中使用它,该库允许我执行类似于JavaScript的[x]. getelementsbytagname()的HTML元素检索。我只是拼接了我在正则表达式的DEFINE部分中编写的功能,并使用它来进入元素树,一次一个。

那么,这将是验证HTML的最终100%答案吗?不。但这只是个开始,只要再努力一点,就可以做到。然而,试图在一个正则表达式执行中完成它是不实际的,也不有效。

其他回答

正则表达式无法解析整个HTML,因为它依赖于匹配开始标记和结束标记,而正则表达式则无法匹配。

正则表达式只能匹配常规语言,但HTML是一种与上下文无关的语言,而不是常规语言(正如@StefanPochmann所指出的,常规语言也是与上下文无关的,因此与上下文无关并不一定意味着不常规)。在HTML上使用regexp唯一能做的事情是启发式,但这并不适用于所有条件。任何正则表达式都可以错误地匹配HTML文件。

这个表达式从HTML元素中检索属性。它支持:

未加引号/加引号的属性, 单引号/双引号, 属性中的转义引号, 等号周围的空格, 任意数量的属性, 只检查标签内的属性, 转义注释,以及 在一个属性值中管理不同的引号。

(?:\<\!\-\-(?:(?!\-\-\>)\r\n?|\n|。)*?-\-\>)|(?:<(\ S+)\s+(?=.*>)|(?<=[=\s])\G)(?:((?:(?!\s|=).)*)\s*?=\ s*?[\"']?((?:(?<=\"))(?:(?<=\\)\"|[^\"])*|(?<=')(? : (?<=\\)'|[^'])*)|(?:(?!\"|')(?:(?!\/>|>|\s).)+)[\ "']?\s*)

来看看。在演示中,使用“gisx”标志效果更好。

就解析而言,正则表达式在“词法分析”(lexer)阶段很有用,在这个阶段,输入被分解成标记。它在实际的“构建解析树”阶段用处不大。

对于HTML解析器,我希望它只接受格式良好的HTML,而这需要正则表达式所不能做到的功能(它们不能“计数”并确保给定数量的开始元素与相同数量的结束元素相平衡)。

请记住,虽然HTML本身不是规则的,但您正在查看的页面的某些部分可能是规则的。

例如,<form>标签被嵌套是一个错误;如果网页正常工作,那么使用正则表达式获取<form>将是完全合理的。

I recently did some web scraping using only Selenium and regular expressions. I got away with it because the data I wanted was put in a <form>, and put in a simple table format (so I could even count on <table>, <tr> and <td> to be non-nested--which is actually highly unusual). In some degree, regular expressions were even almost necessary, because some of the structure I needed to access was delimited by comments. (Beautiful Soup can give you comments, but it would have been difficult to grab <!-- BEGIN --> and <!-- END --> blocks using Beautiful Soup.)

但是,如果我不得不担心嵌套表,那么我的方法根本就行不通!我就只能靠《美丽汤》了。但是,即使这样,有时也可以使用正则表达式获取所需的块,然后从那里展开。

Actually, HTML parsing with regex is perfectly possible in PHP. You just have to parse the whole string backwards using strrpos to find < and repeat the regex from there using ungreedy specifiers each time to get over nested tags. Not fancy and terribly slow on large things, but I used it for my own personal template editor for my website. I wasn't actually parsing HTML, but a few custom tags I made for querying database entries to display tables of data (my <#if()> tag could highlight special entries this way). I wasn't prepared to go for an XML parser on just a couple of self created tags (with very non-XML data within them) here and there.

所以,即使这个问题已经死了,它仍然会出现在谷歌搜索中。我读了它,并认为“接受挑战”,并完成了修复我的简单代码,而不需要替换所有东西。决定给有类似理由的人提供不同的意见。最后一个答案是4小时前发布的,所以这仍然是一个热门话题。