Question

I have a string which looks like this:

43.3433443 25.3434334), (43.32424432 25.435345345, 43.21313123, 25.32432423423), (43.123123123 25.344234234, 43.1234123123 25.23213123))

I need to extract everything that is between ( and ) as well as ( and )) with preg_match_all

I have the following regex, which works for extracting everything between ( and ) but not ( and ))

preg_match_all('/\), \(([A-Za-z0-9., ]+?)\)/', $data[$x], $inner);

I've tried using:

preg_match_all('/\), \(([A-Za-z0-9., ]+?)\)|\), \(([A-Za-z0-9., ]+?)\)\)/', $data[$x], $inner);

and:

preg_match_all('/(?=\), \(([A-Za-z0-9., ]+?)\))(?=\), \(([A-Za-z0-9., ]+?)\)\))/', $data[$x], $inner);

But none of them have worked. How would I go about to include both cases?

Thank you.

UPDATE:

I forgot to mention, the string contains numbers within multiple starting and ending parentheses that I don't want, i.e. two parantheses ---> (( numbers here ))) <---3 parantheses

Was it helpful?

Solution

Use following regex:

([^()]+?)\)

Live demo

PHP code:

preg_match_all('/([^()]+?)\)/', $data[$x], $inner);

Regex for your last update:

([^()]+?)\)(?![)]{2,})

Live demo

OTHER TIPS

I may be wrong in this, but () can be found in ()), so why not just look for () and be done:

<?php
$str = '43.3433443 25.3434334), (43.32424432 25.435345345, 43.21313123, 25.32432423423), (43.123123123 25.344234234, 43.1234123123 25.23213123))';

preg_match_all("/\((.*?)\)/",$str,$matches);

echo '<pre>',print_r($matches),'</pre>';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top