Question

I have a specific requirement to parse some basic pseudo code in (wait for it) PHP. The type of string that might need parsing is as follows:

MID(DATEDIFF(ymd,131001,131201),0,1)

Such that:

DATEDIFF(ymd,131001,131201) = 61
MID(60,0,1) = 6

The parsing of the individual expressions is not the problem, the problem is extracting the nested function calls with regex. I would really appreciate some input that might put me on the right track to be able to extract the function calls and their nested index - i.e.: Find the calls for MID (index = 0) and DATEDIFF (index = 1), which would allow me to evaluate the highest indexes first adn use those results in the lower indexes.

The best i can do with my feeble PCRE skills is the following:

$expression = 'MID(DATEDIFF(dmy,011213,021213),0,1)';
$regexp = '/^(DATEDIFF\(|MID\()(.*)\)/';
if (preg_match_all($regexp, $expression, $matches, PREG_OFFSET_CAPTURE)) {
    var_dump($matches); 
else echo "no match" . PHP_EOL;
Was it helpful?

Solution

Have a look to this regex:

(?P<function>\w+)(?=
    (?P<body>
       \(
          (?:
            [^()]
            |
            (?&body)
          )*
       \)
    )
)

Description

Regular expression visualization

Demo

http://regex101.com/r/sY3eG0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top