加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用Regex匹配函数体

发布时间:2020-12-13 21:55:53 所属栏目:PHP教程 来源:网络整理
导读:给定一个虚函数: public function handle(){ if (isset($input['data']) { switch($data) { ... } } else { switch($data) { ... } }} 我的目的是获取该函数的内容,问题是匹配花括号{…}的嵌套模式. 我遇到了recursive patterns,但无法理解一个与函数体相匹
给定一个虚函数:

public function handle()
{
  if (isset($input['data']) {
    switch($data) {
      ...
    }
  } else {
    switch($data) {
      ...
    }
  }
}

我的目的是获取该函数的内容,问题是匹配花括号{…}的嵌套模式.

我遇到了recursive patterns,但无法理解一个与函数体相匹配的正则表达式.

我尝试了以下(没有递归):

$pattern = "/functionshandle([a-zA-Z0-9_$s,]+)?". // match "function handle(...)"
            '[ns]?[ts]*'. // regardless of the indentation preceding the {
            '{([^{}]*)}/'; // find everything within braces.

preg_match($pattern,$contents,$match);

这种模式根本不匹配.我确信它是最后一个错误的'{([^ {}] *)} /’,因为该模式在正文中没有其他大括号时起作用.

将其替换为:

'{([^}]*)}/';

它匹配到if语句中的开关的关闭}并且在那里停止(包括}但不包括if的那个).

除了这种模式,同样的结果:

'{(K[^}]*(?=)})/m';

解决方法

更新#2

根据其他人的评论

^s*[ws]+(.*)s*K({((?>"(?:[^"]*+|.)*"|'(?:[^']*+|.)*'|//.*$|/*[sS]*?*/|#.*$|<<<s*["']?(w+)["']?[^;]+3;$|[^{}<'"/#]++|[^{}]++|(?1))*)})

注意:如果您知道您的输入不包含PHP语法中的{或},那么简短的RegEx即{((?> [^ {}] |(?R))*)}就足够了.

那么一个长期的RegEx,它在什么邪恶案件中起作用?

>引号[“’]之间的字符串中有[{}]个
>你有这些引号在彼此内部逃脱
>评论栏中有[{}]个. // …或/*…*/或#…
>你有[{}] in heredoc或nowdoc<<<< STR或<<<< ['“] STR ['”]
否则它意味着有一对打开/关闭支撑和嵌套支撑的深度并不重要.

我们有一个失败的案例吗?

不,除非你的代码内有火星.

^ s* [ws]+ ( .* ) s* K               # how it matches a function definition
 (                             # (1 start)
      {                                      # opening brace
      (                             # (2 start)
           (?>                               # atomic grouping (for its non-capturing purpose only)
                "(?: [^"]*+ |  . )*"     # double quoted strings
             |  '(?: [^']*+ |  . )*'     # single quoted strings
             |  // .* $                     # a comment block starting with //
             |  /* [sS]*? */             # a multi line comment block /*...*/
             |  # .* $                     # a single line comment block starting with #...
             |  <<< s* ["']?                # heredocs and nowdocs
                ( w+ )                      # (3) ^
                ["']? [^;]+ 3 ; $          # ^
             |  [^{}<'"/#]++                 # force engine to backtack if it encounters special characters [<'"/#] (possessive)
             |  [^{}]++                      # default matching bahaviour (possessive)
             |  (?1)                         # recurse 1st capturing group
           )*                                # zero to many times of atomic group
      )                             # (2 end)
      }                                      # closing brace
 )                             # (1 end)

格式化由@ sln的RegexFormatter软件完成.

我在现场演示中提供了什么?

随机给出Laravel的Eloquent Model.php文件(~3500行)作为输入.看看这个:
Live demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读