I want to get an array-formatted substring which is inside of the input()
. I used preg_match
but can't get the entire expression. It is stopping at the first )
. How can I match the entire substring? Thank You.
$input="input([[1,2,nc(2)],[1,2,nc(1)]])";
preg_match('@^([^[]+)?([^)]+)@i',$input, $output);
Expectation is:
'[[1,2,nc(2)],[1,2,nc(1)]]'
This pattern match your desired string (also with starting word ≠ ‘input’:
@^(.+?)\((.+?)\)$@i
^(.+?) => find any char at start (ungreedy option)
\) => find one parenthesis
(.+?) => find any char (ungreedy option) => your desired match
\) => find last parenthesis