php - Regular expression to take value between brackets "()"? -


i poor in writing regular expression.

i trying value between brackets "()". below...

$a = "polygon((1 1,2 2,3 3,1 1))"; preg_match_all("/\((.*)\)/u", $a, $pat_array); print_r($pat_array); 

but give me elements like...

array (     [0] => array         (             [0] => ((1 1,2 2,3 3,1 1)         )      [1] => array         (             [0] => (1 1,2 2,3 3,1 1         ) ) 

but want fetch "1 1,2 2,3 3,1 1" output.

i know can trim brackets after getting output. great if done using regular expression.

thanks in advance.


solution: per @anubhava answer:

using @anubhava's regular expression.

example:

$a = "polygon((1 1,2 2,3 3,1 1),(1 1,2 2,3 3,1 1),(1 1,2 2,3 3,1 1))"; 

and if print result of regular expression this...

array (     [0] => array         (             [0] => ((1 1,2 2,3 3,1 1)             [1] => (1 1,2 2,3 3,1 1)             [2] => (1 1,2 2,3 3,1 1))         )      [1] => array         (             [0] => 1 1,2 2,3 3,1 1             [1] => 1 1,2 2,3 3,1 1             [2] => 1 1,2 2,3 3,1 1         ) ) 

look @ second array element, it's want.

fyi: have used fetching geometry data of polygon mysql database , processed array latitude , longitude of polygon nodes.

use negation based regex pattern , result captured group #1:

preg_match_all('/\(+([^()]*)\)+/', $a, $pat_array); print_r($pat_array[1]); 

[^()]* match char not ( or ).

output:

array (     [0] => 1 1,2 2,3 3,1 1 ) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -