How to get method/function in a string by using regex

Viewed 84

I trying to get arguments from function in the string.

Argument possible to contains:

Example

Placeholder:

{{request.expires_in}}
//can match regex: \{\{[a-zA-z0-9\.\-\_]\}\}

function

@func_compare('string1','string2',1234)

Others:

dERzUxOfnj49g/kCGLR3vhzBOTLwEMgrpa1/MCBpXQR2NIFV1yjraGVZLkujG63J0joj+TvNocjpJSQq2TpPRzLfCSZADcjmbkBkphIpsT8=
//Any string except brackets

Case

Below is the sample case I working with.

Content:

@func_compare('string1',@func_test(2),1234),'Y-m-d H:i:s',@func_strtotime({{request.expires_in}} - 300)

Regex using:

(?<=@func_compare\().*[^\(](?=\))

I expect will get

'string1',@func_test(2),1234

But what matched from the regex now is

'string1',@func_test(2),1234),'Y-m-d H:i:s',@func_strtotime({{request.expires_in}} - 300

Anyone know how to get the arguments in between the @func_strtotime brackets. I will appreciate any response.

2 Answers

Would you please try:

(?<=@func_compare\().*?(?:\(.*?\).*?)?(?=\))

which will work for both cases.

[Explanation of the regex]

.*?(?:\(.*?\).*?)?(?=\))

.*?                       the shortest match not to overrun the next pattern
   (?:\(.*?\).*?)?        a group of substring which includes a pair of parens followed by another substring of length 0 or longer
                  (?=\))  positive lookahead for the right paren

You'll get the result using recursive regex:

(?<=@func_compare\()([^()]*\((?:.*?\)|(?1))*)[^()]*(?=\))

Demo & explanation

Related