duan0417 2015-07-23 07:52
浏览 49

PHP preg_match:基于缩进提取完整的javascript函数

I have an input like this

var example = ...

var foobar = function() {
    var barfoo = function() {

    }
}


var something = ...

I want to extract the function foobar.

How can I extract the text from var foobar until a closing bracket which is at the first indentation level?

preg_match('#var foobar =.*^}/#ms', $this->body, $matches);

Does not give any matches.

  • 写回答

1条回答 默认 最新

  • duanou1904 2015-07-23 08:02
    关注

    This returns what you want:

    preg_match('#var foobar =.*}#ms', $this->body, $matches);
    

    But it will not work if you fill the dots at the end with more functions.

    Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion.

    Regular Expression to match outer brackets

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考