【问题标题】:Understanding Pattern in preg_match_all() Function Call了解 preg_match_all() 函数调用中的模式
【发布时间】:2012-02-05 14:41:49
【问题描述】:

我试图了解preg_match_all() 的工作原理,在查看 php.net 站点上的文档时,我看到了一些示例,但对作为模式参数发送的字符串感到困惑。那里有一个非常彻底,清晰的解释吗?例如,我不明白这个例子中的模式是什么意思:

preg_match_all("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
            "Call 555-1212 or 1-800-555-1212", $phones);

或者这个:

$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);

我上过关于 PHP 的介绍性课程,但从未见过这样的事情。一些澄清将不胜感激。

谢谢!

【问题讨论】:

标签: php regex preg-match-all


【解决方案1】:

那些不是“PHP 模式”,它们是正则表达式。我不会尝试在此答案中解释一千次之前已经解释过的内容,而是将您指向http://regular-expressions.info 以获取信息和教程。

【讨论】:

    【解决方案2】:

    你正在寻找这个,

    1. PHP PCRE Pattern Syntax
    2. PCRE Standard syntax

    请注意,第一个是第二个的子集。

    【讨论】:

      【解决方案3】:

      还可以查看YAPE,例如,它为您的第一个正则表达式提供了很好的文字解释:

      (?x-ims:\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4})
      
      matches as follows:
      
      NODE                     EXPLANATION
      ----------------------------------------------------------------------
      (?x-ims:                 group, but do not capture (disregarding
                               whitespace and comments) (case-sensitive)
                               (with ^ and $ matching normally) (with . not
                               matching \n):
      ----------------------------------------------------------------------
        \(?                      '(' (optional (matching the most amount
                                 possible))
      ----------------------------------------------------------------------
        (                        group and capture to \1 (optional
                                 (matching the most amount possible)):
      ----------------------------------------------------------------------
          \d{3}                    digits (0-9) (3 times)
      ----------------------------------------------------------------------
        )?                       end of \1 (NOTE: because you are using a
                                 quantifier on this capture, only the LAST
                                 repetition of the captured pattern will be
                                 stored in \1)
      ----------------------------------------------------------------------
        \)?                      ')' (optional (matching the most amount
                                 possible))
      ----------------------------------------------------------------------
        (?(1)                    if back-reference \1 matched, then:
      ----------------------------------------------------------------------
          [\-\s]                   any character of: '\-', whitespace (\n,
                                   \r, \t, \f, and " ")
      ----------------------------------------------------------------------
         |                        else:
      ----------------------------------------------------------------------
                                   succeed
      ----------------------------------------------------------------------
        )                        end of conditional on \1
      ----------------------------------------------------------------------
        \d{3}                    digits (0-9) (3 times)
      ----------------------------------------------------------------------
        -                        '-'
      ----------------------------------------------------------------------
        \d{4}                    digits (0-9) (4 times)
      ----------------------------------------------------------------------
      )                        end of grouping
      ----------------------------------------------------------------------
      

      【讨论】:

      • 你究竟是从哪里得到这个输出的?我查看了您提供的链接,但它只是 Google 搜索,我并没有真正找到可能产生此输出的内容。
      • 是的,这是第一个链接,Perl 模块。为此,我为自己制作了一个小小的 shell 脚本。这只是perl -e " use YAPE::Regex::Explain; my \$re = qr{$1}$2; print YAPE::Regex::Explain-&gt;new(\$re)-&gt;explain(); "——但你也可以继续重写那个小示例脚本,就像在它的CPAN page 上看到的那样。
      • 这是不对的。 OP 的正则表达式使用 /x 修饰符,因此第一个节点应该是 (?x-ims: 并且不应列出那些纯空白节点。但无论如何,这份清单是不完整的。根据this bug report 的说法,该模块自 Perl 5.6 以来一直没有更新,并且 PCRE 一开始总是支持一组略有不同的修饰符。
      • @AlanMoore:是的,更新为实际指定x。无论如何,它仅用于说明目的。它似乎仍然适用于许多 PCRE 模式,但显然它不是最漂亮的工具。
      【解决方案4】:

      您所写的模式是一种称为正则表达式的迷你语言。它专门用于查找字符串中的模式,对遵循某种模式的所有内容进行替换等。

      更具体地说,它是一个 Perl 兼容的正则表达式 (PCRE)。

      PHP 手册网站上没有该语言的手册,您可以在此处找到:PCRE Manpage

      Regular Expressions Info Website 上有一个精心制作的分步介绍。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-20
        • 1970-01-01
        • 2019-06-20
        相关资源
        最近更新 更多