理解preg_match_all()函数调用中的模式


Understanding Pattern in preg_match_all() Function Call

我正在努力了解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模式",它们是正则表达式。与其试图解释这个答案中已经解释了一千次的内容,我将向您指出http://regular-expressions.info以获取信息和教程。

你正在寻找这个,

  1. PHP PCRE模式语法
  2. PCRE标准语法

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

还可以看看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
----------------------------------------------------------------------

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

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

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

正则表达式信息网站上有一个精心制作的逐步介绍。