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

时间:2012-01-08 22:44:33

标签: php regex preg-match-all

我试图了解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的入门课程,但从未见过这样的东西。一些澄清将不胜感激。

谢谢!

4 个答案:

答案 0 :(得分:4)

那些不是“PHP模式”,那些是正则表达式。在这个答案中,我没有试图解释一千次解释过的内容,而是指向http://regular-expressions.info获取信息和教程。

答案 1 :(得分:3)

你正在寻找这个,

  1. PHP PCRE Pattern Syntax
  2. PCRE Standard syntax
  3. 请注意,第一个是第二个的子集。

答案 2 :(得分:2)

另请查看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
----------------------------------------------------------------------

答案 3 :(得分:1)

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

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

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

精心制作的逐步介绍在Regular Expressions Info Website上。