开发者

How to understand the output of the PHP regex matching below?

$file = '{include file="{$COMMON_TPL_PATH}common/header_admin.tpl"}
{include file="{$mainPage}"}
{include file="{$COMMON_TPL_PATH}common/footer_admin.tpl"}';
preg_match('/^(\{\})|^(\{\*([\S\s]*?)\*\})|^(<\?(?:php\w+|=|[a-zA-Z]+)?)|^([     ]*[
]+[  ]*)|^(\{strip\})|^(\{\/strip\})|^(\{literal\})|^(\{\s{1,}\/)|^(\{\s{1,})|^(\{\/)|^(\{)|^(([\S\s]*?)(?=([    ]*[
]+[  ]*|\{|<\?)))|^([\S\s]+)/', $file开发者_如何学运维, $matches);
var_dump($matches);

Why the output is:

array(13) {
  [0]=>
  string(1) "{"
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
  [3]=>
  string(0) ""
  [4]=>
  string(0) ""
  [5]=>
  string(0) ""
  [6]=>
  string(0) ""
  [7]=>
  string(0) ""
  [8]=>
  string(0) ""
  [9]=>
  string(0) ""
  [10]=>
  string(0) ""
  [11]=>
  string(0) ""
  [12]=>
  string(1) "{"
}

It seems to me that ^([\S\s]+) can match the whole string..


The reason is that group 12 (^(\{)) matches successfully, and therefore the regex engine stops. It never even gets to try to match group 16 (^([\S\s]+)).

If you put group 16 before group 12, then it will match the entire string. However, since [\s\S] matches any character at all (and can be abbreviated to . when using the s modifier), then none of the other groups will ever match.

All in all, your regex looks quite weird. I wouldn't be surprised if it contained a few bugs aside from the obvious warts like trying to put several spaces into a single character class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜