我看到人们反复犯的一个错误是试图用正则表达式解析XML或HTML。以下是解析XML和HTML比较困难的几个原因:

人们希望将文件视为行序列,但这是有效的:

<tag
attr="5"
/>

人们希望将<或<标记作为标记的开始,但这样的东西在野外存在:

<img src="imgtag.gif" alt="<img>" />

人们经常想要匹配开始标签和结束标签,但是XML和HTML允许标签包含自己(传统的正则表达式根本无法处理):

<span id="outer"><span id="inner">foo</span></span> 

人们经常想要匹配文档的内容(例如著名的“在给定页面上查找所有电话号码”问题),但数据可能会被标记(即使在查看时看起来是正常的):

<span class="phonenum">(<span class="area code">703</span>)
<span class="prefix">348</span>-<span class="linenum">3020</span></span>

注释可能包含格式不佳或不完整的标记:

<a href="foo">foo</a>
<!-- FIXME:
    <a href="
-->
<a href="bar">bar</a>

你还知道其他什么陷阱吗?


当前回答

下面是一些有趣的有效XML:

<!DOCTYPE x [ <!ENTITY y "a]>b"> ]>
<x>
    <a b="&y;>" />
    <![CDATA[[a>b <a>b <a]]>
    <?x <a> <!-- <b> ?> c --> d
</x>

这个快乐的小包裹是有效的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" [
    <!ENTITY % e "href='hello'">
    <!ENTITY e "<a %e;>">
]>
    <title>x</TITLE>
</head>
    <p id  =  a:b center>
    <span / hello </span>
    &amp<br left>
    <!---- >t<!---> < -->
    &e link </a>
</body>

更不用说所有针对无效结构的浏览器特定解析。

祝你在正则表达式与之对抗时好运!

EDIT (Jörg W Mittag):这是另一个格式良好、有效的HTML 4.01:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd"> 
<HTML/
  <HEAD/
    <TITLE/>/
    <P/>

其他回答

人们通常默认编写贪婪模式,通常足以导致未经深思熟虑的。*将大块文件吸进尽可能大的<foo>.*</foo>. *。

我相信这本经典书里有你要找的信息。你可以在那里的一条评论中找到要点:

我认为这里的缺陷在于HTML是Chomsky Type 2语法 (上下文无关语法),RegEx是Chomsky Type 3语法(常规语法) 表达式)。因为Type 2语法从根本上比 第三型语法——你不可能指望能做到这一点。但许多 会尝试,有人会声称成功,有人会发现错误和 把你搞得一团糟。

更多信息来自维基百科:乔姆斯基等级

这取决于你对“解析”的定义。一般来说,XML不能使用正则表达式来解析,因为XML语法根本不是规则的。简单地说,正则表达式无法计数(好吧,Perl的正则表达式实际上可能能够计数),因此您无法平衡打开-关闭标记。

我认为问题可以归结为:

The regex is almost invariably incorrect. There are legitimate inputs which it will fail to match correctly. If you work hard enough you can make it 99% correct, or 99.999%, but making it 100% correct is almost impossible, if only because of the weird things that XML allows by using entities. If the regex is incorrect, even for 0.00001% of inputs, then you have a security problem, because someone can discover the one input that will break your application. If the regex is correct enough to cover 99.99% of cases then it is going to be thoroughly unreadable and unmaintainable. It's very likely that a regex will perform very badly on moderate-sized input files. My very first encounter with XML was to replace a Perl script that (incorrectly) parsed incoming XML documents with a proper XML parser, and we not only replaced 300 lines of unreadable code with 100 lines that anyone could understand, but we improved user response time from 10 seconds to about 0.1 seconds.

一般来说,XML不能使用正则表达式来解析,因为XML语法根本不是规则的。简单地说,正则表达式无法计数(好吧,Perl的正则表达式实际上可能能够计数),因此您无法平衡打开-关闭标记。

我不同意。如果你要在regex中使用递归,你可以很容易地找到打开和关闭标记。

这里我展示了一个regex的例子,以避免在第一条消息中解析例子的错误。