Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 理解preg_match_all()函数调用中的模式_Php_Regex_Preg Match All - Fatal编程技术网

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

Php 理解preg_match_all()函数调用中的模式,php,regex,preg-match-all,Php,Regex,Preg Match All,我试图理解preg\u match\u all()是如何工作的,当我查看php.net站点上的文档时,我看到了一些示例,但对作为模式参数发送的字符串感到困惑。有没有一个真正彻底、清晰的解释?例如,我不明白本例中的模式是什么意思: preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212", $phones); 或者这个: $h

我试图理解
preg\u match\u 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);
$html=“粗体文本”;
preg_match_all(“/(]*>)(*?)(*)/”,$html,$matches,preg_SET_ORDER);
我上过PHP入门课,但从未见过这样的课程。请作一些澄清


谢谢

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

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

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

上有一个很好的分步介绍。

这些不是“PHP模式”,而是正则表达式。在这个答案中,我不再试图解释之前已经解释过一千遍的内容,而是让您参考一些信息和教程。

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

(?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模块。我为自己做了一个小小的脚本。它只是
    perl-e“使用YAPE::Regex::Explain;my\$re=qr{$1}$2;print YAPE::Regex::Explain->new(\$re)->Explain();”
    ——但是你也可以继续重写它上面看到的小示例脚本。这是不对的。OP的正则表达式使用
    /x
    修饰符,因此第一个节点应该是
    (?x-ims:
    和那些纯空白节点不应列出。但该列表无论如何都是不完整的。根据,该模块自Perl 5.6以来从未更新过,并且PCRE始终支持一组稍有不同的修饰符作为开头。@Alanmore:True,更新时实际指定了
    x
    。它仅用于说明p不管怎样,它似乎仍然适用于许多PCRE模式,但显然它不是最漂亮的工具。