Octavian
Octavian

Reputation: 155

Find the regular expression

There is a line containing this:

function dekorator($func, $agrs = [])
{
    if (dd){
        var_dump("dekorators");

        $func();
    }
}

if (true)
{
    @dekorator
    function test()
    {
        if (ss){
            var_dump("tests");
            while(){
                dd
            }
        }
    }

    function dgdg()
    {
        var_dump("ff");
    }

    test();
}

I need to get the following:

  1. Function name
  2. Function Parameters
  3. The function body (inside the quotes {} relating only to the function)

I wrote a regular expression:

/function?\s+(\w+)*\(([^\)]*)\).*?\{([^\}]+[^\{]+)\}/sim

But there is one BUT, the last function it captures is not the quotation mark (which concerns not the function itself)

The function itself:

function dgdg()
{
    var_dump("ff");
}

Superfluous:

    test();
}

DEMO: https://regex101.com/r/MTS1pr/1/

Upvotes: 0

Views: 86

Answers (1)

zen
zen

Reputation: 992

Use this expression:

function\s+(\w*)\s*\((.*?)\)\s*(\{((?>[^{}]+|(?3))*)\})

Demo: https://regex101.com/r/o0t3fY/2

Upvotes: 1

Related Questions