2

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

alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
  • 1
    Euh, that's some crazy input ... Why don't you [just do](http://regex101.com/r/fJ4lR5) `\([^)]*\){1,2}` ? – HamZa Jan 22 '14 at 20:26
  • Do you only need the numbers? Then, `preg_match_all('/\d+\.\d+/', $str, $matches);` should do the job: https://eval.in/93503 – ComFreek Jan 22 '14 at 20:27
  • @HamZa - post that as an answer, it works. – Surreal Dreams Jan 22 '14 at 20:29
  • Looks nice, and is way more efficient than my version of it, but alas, it only matches between ( and ), not between ( and )) – alexisdevarennes Jan 22 '14 at 20:31
  • @ComFreek - I will check it out, I need the commas as well. – alexisdevarennes Jan 22 '14 at 20:32
  • @user2973474 I sometimes "sense" [chameleon questions](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) ... – HamZa Jan 22 '14 at 20:41
  • @HamZa, not at all. Honestly, I just have no experience whatsoever with regex and I wouldn't ask for help if I hadn't been on this for hours now. – alexisdevarennes Jan 22 '14 at 20:43
  • @user2973474 By chameleon question I mean that it changes. Do you know how tiresome it is to edit a [detailed answer](http://stackoverflow.com/a/18217486) ? Anyways, let's make it clear: Does your first example really not start with `(` ? Could you add some clear input and expected output ? What happens when using `(((( 1, 3, 10)))))`? – HamZa Jan 22 '14 at 20:45
  • updated my answer to your last edit. – revo Jan 22 '14 at 20:49

2 Answers2

3

Use following regex:

([^()]+?)\)

Live demo

PHP code:

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

Regex for your last update:

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

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117
1

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>';
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • This doesn't work (I've not downvoted, would have done the same and I'm currently unsure why it doesn't work) – hek2mgl Jan 22 '14 at 20:34
  • This would work except, I forgot to mention (Sorry), the string contains some numbers within brackets I don't want, i.e. (( numbers here ))) – alexisdevarennes Jan 22 '14 at 20:36