How to capture an array-formatted substring from a string

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.

preg_match('@^([^[]+)?([^)]+)@i',$input, $output); 

Expectation is:



This pattern match your desired string (also with starting word ≠ ‘input’:

@^(.+?)\((.+?)\)$@i demo

^(.+?)   => find any char at start (ungreedy option)
\)       => find one parenthesis 
(.+?)    => find any char (ungreedy option) => your desired match
\)       => find last parenthesis

Try this one:


$matches[1] will contain whole result you need. Hope this works.

You want it purely as a string? Use this simple regex:


None of the other answers have efficiently/accurately answered your question:

For the fastest accurate pattern, use:

echo preg_match('/input\((.*)\)/i',$input,$output)?$output[1]:'';
//                                            notice index ^

Or a slightly slower pattern that uses 50% less memory by avoiding the capture group, use:

echo preg_match('/input\(\K(.*)(?=\))/i',$input,$output)?$output[0]:'';
//                                                  notice index ^

Both methods will provide the same output: [[1,2,nc(2)],[1,2,nc(1)]]

Using a greedy * quantifier allows the pattern to move passed the nested parentheses and match the entire intended substring.

In the second pattern, \K resets the match's starting point and (?=\)) is a positive lookahead that ensures the entire substring is matched without including the trailing closing parenthesis.

EDIT: All that regex convolution aside, because you know your desired substring is wrapped in input( and ), the best, simplest approach is a non-regex one...

echo substr($input,6,-1);
// output: [[1,2,nc(2)],[1,2,nc(1)]]


Need Your Help

5 columns in bootstrap impossible?

html css twitter-bootstrap twitter-bootstrap-3

Client has 5 pricing options. I'm using wells to display the price points. since Bootstrap uses columns instead of percentages, is it not possible to create even columns out of the box?

GWT JSNI method with native JS: "Invalid Label"

java javascript gwt jsni codemirror

Maybe my question is simple to solve for you, but I'm sitting here for a few hours with following problem (and I have already searched for it):