任何人都可以帮我解决我的正则表达式问题吗?

时间:2013-10-16 23:01:04

标签: php regex

我正在尝试使用正则表达式从HTML文件中提取多个URL。 HTML代码如下所示:

<h1 class="article"><a href="http://www.domain1.com/page-to-article1" onmousedown="return(...)
<h1 class="article"><a href="http://www.domain2.com/page-to-article2" onmousedown="return(...)
<h1 class="article"><a href="http://www.domain3.com/page-to-article3" onmousedown="return(...)
<h1 class="article"><a href="http://www.domain3.com/page-to-article4" onmousedown="return(...)

我想仅在<h1 class="article"><a href="" onmousedown="return(...)之间提取网址,例如http://www.domain1.com/page-to-article1http://www.domain2.com/page-to-article2http://www.domain3.com/page-to-article3等等。

1 个答案:

答案 0 :(得分:4)

正如已经回答和评论的那样,您不应该使用正则表达式执行此任务。但是,如果你真的坚持它,你可以使用这个正则表达式:

/\<h1 class="article"\>\<a href="([^"]*)" onmousedown="return/

创建此正则表达式的演练:

  1. 嗯,你究竟在寻找什么?像这一行:

    <h1 class="article"><a href="http://www.domain1.com/page-to-article1" onmousedown="return
    
  2. 但是,正则表达式中不允许使用某些字符。在此示例中,<>字符是非​​法的。因此,您应该通过在非法字符前添加反斜杠(\)来逃避它们:

    \<h1 class="article"\>\<a href="http://www.domain1.com/page-to-article1" onmousedown="return
    
  3. 这只会匹配正则表达式中已有的网址。我们想要匹配任何网址。通常,URL如何在此上下文中查找?这很难说,因为URL存在许多不同的形式。

    一个简单的描述是:URL是一堆不包含"字符的文本(因为它会结束href标记的<a>属性。在正则表达式中,这将是[^"]:它匹配除"之外的任何字符。

    我们还没有完成某件事:除了"之外,URL不仅仅是一个字符,而是一大堆字符。因此,我们在模式(*)中添加一个星号([^"]),该模式匹配零个或多个字符。这导致[^"]*。现在可以匹配任何长度的URL。

    我们不应该忘记我们实际上想要从文本中获取URL(而不仅仅是匹配/检测它)。通过定义组,将单独返回组的内容。您可以通过将模式放在括号中来定义组。结果:([^"]*)

    现在我们可以将其替换为我们开始使用的模式:

    \<h1 class="article"\>\<a href="([^"]*)" onmousedown="return
    
  4. 我们应该做的最后一件事是告诉正则表达式处理器我们是否要匹配整行(即,只有在我们的模式匹配整行时才能找到结果)或者部分行。我们选择后者。为此,我们将模式放在斜杠中:

    /\<h1 class="article"\>\<a href="([^"]*)" onmousedown="return/
    
  5. 在最后一步中,我们可以添加修饰符。这些就像正则表达式处理器在匹配您的模式时使用的偏好。我们添加i修饰符,以使模式不区分大小写:

    /\<h1 class="article"\>\<a href="([^"]*)" onmousedown="return/i
    
  6. 我建议您查看regex cheat sheet并尝试了解正则表达式中发生的情况。将其添加到书签(或打印)。无论何时遇到正则表达式或需要自己的正则表达式,请尝试使用它。如果你是新手,正则表达似乎是一种难以理解的魔法,但如果你学会自己正确地使用它们,这将非常方便。


    使用示例:

    <?php
    
    $html = <<<EOF
    <h1 class="article"><a href="http://www.domain1.com/page-to-article1" onmousedown="return(...)
    <h1 class="article"><a href="http://www.domain2.com/page-to-article2" onmousedown="return(...)
    <h1 class="article"><a href="http://www.domain3.com/page-to-article3" onmousedown="return(...)
    <h1 class="article"><a href="http://www.domain3.com/page-to-article4" onmousedown="return(…)
    EOF;
    
    preg_match_all('/\<h1 class="article"\>\<a href="([^"]*)" onmousedown="return/i', $html, $matches);
    
    print_r($matches[1]);
    // Array
    // (
    //     [0] => http://www.domain1.com/page-to-article1
    //     [1] => http://www.domain2.com/page-to-article2
    //     [2] => http://www.domain3.com/page-to-article3
    //     [3] => http://www.domain3.com/page-to-article4
    // )
    
    ?>