【发布时间】:2013-09-10 16:49:15
【问题描述】:
如果字符串是
<li>Your browser may be missing a required plug-in contained in <a href="http://get.adobe.com/reader/">Adobe Acrobat Reader</a>. Please reload this page after installing the missing component.<br />If this error persists, you can also save a copy of <a href="test.pdf">
我写的正则表达式是
/href=.*?.pdf/
这会导致捕获第一个“href”并以“.pdf”结尾。我需要它从第二个 href 开始。换句话说,它应该只捕获以 .pdf 结尾的 href
我应该如何使用正则表达式来解决这个问题?
【问题讨论】:
-
不要使用正则表达式解析 HTML。使用适当的 HTML 解析模块。 您无法使用正则表达式可靠地解析 HTML,并且您将面临悲伤和挫败感。一旦 HTML 与您的期望发生变化,您的代码就会被破坏。请参阅htmlparsing.com/php 或this SO thread,了解如何使用已经编写、测试和调试过的 PHP 模块正确解析 HTML。
标签: php regex html-parsing