Minase Posted January 5, 2009 Share Posted January 5, 2009 hy there i have the following text (game (name test) (valueb something) (etc) (etc) (etc)) (game (name test2) (valueb something) (etc) (etc) (etc)) // this is just a 3 line example ,it can be on 1 line or on 10 //thats how a doccument will look,i need to group everything from "(group till the two ))" anyone know how i can do this with regex? thank you Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/ Share on other sites More sharing options...
.josh Posted January 6, 2009 Share Posted January 6, 2009 so you want to capture this: (game (name test) (valueb something) (etc) (etc) (etc)) Do you want to capture it as just 1 string of info, or each individual (...)? Your example has two sets of those things; will your document have more than 1, and do you want to capture those, as well? Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/#findComment-730571 Share on other sites More sharing options...
Minase Posted January 6, 2009 Author Share Posted January 6, 2009 yes i have multiple and i want to capture them into a string thank you Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/#findComment-730624 Share on other sites More sharing options...
.josh Posted January 6, 2009 Share Posted January 6, 2009 Probably not the best way, but... preg_match_all("~\(.+?\)\)~s",$string,$match); print_r($match); Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/#findComment-730659 Share on other sites More sharing options...
sasa Posted January 6, 2009 Share Posted January 6, 2009 or <?php $test = '(game (name test) (valueb something) (etc) (etc) (etc)) (game (name test2) (valueb something) (etc) (etc) (etc)) // this is just a 3 line example ,it can be on 1 line or on 10 //thats how a doccument will look,i (need to group everything from "(group till the two ))" anyone know how i can do this with regex? thank you'; $len = strlen($test); $out = array(); $count = 0; $part = ''; for ($i = 0; $i < $len; $i++){ if ($test[$i] == ')') $count--; if ($count > 0) $part .= $test[$i]; elseif ($part){ $out[] = $part; $part = ''; } if ($test[$i] == '(') $count++; } print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/#findComment-730690 Share on other sites More sharing options...
Minase Posted January 6, 2009 Author Share Posted January 6, 2009 thank you guys the code works perfectly but on single lines only :| how i can make it to search for multiline too it seem that " . character doesnt include \n or \r\n" thanks again Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/#findComment-730726 Share on other sites More sharing options...
Minase Posted January 6, 2009 Author Share Posted January 6, 2009 i did solve it i used tolerance option thank you very much for all your help Quote Link to comment https://forums.phpfreaks.com/topic/139597-solved-regex/#findComment-730797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.