knot13yet Posted January 28, 2012 Share Posted January 28, 2012 I hate rexep(s) so far... I simply need to get these 3 or 4 or 5 or ... sets of numbers example: (0 0) (4 4) (6 6) (2 2) From this: 4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) ) 4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) ) 4 4 0 -1 ( (1 1) (3 3) (7 7) (5 5) ) 4 4 0 -1 ( (0 0) (2 2) (3 3) (1 1) ) 4 4 0 -1 ( (4 4) (5 5) (7 7) (6 6) ) 4 4 0 -1 ( (2 2) (6 6) (7 7) (3 3) ) 4 4 0 -1 ( (0 0) (1 1) (5 5) (4 4) ) These are faces of an Anim8or 3D model. I am importing the file and handling the data in PHP then displaying it in HTML5/jQuery. The jQuery bit works great. The first 4 is number of faces... If that's any help. And it can ary based on the number of faces in the mesh. The next 3 digits are likely for UV ( dunno ), don't need them yet. This corresponds to arrays in my jquery later that specify the lines. psuedocode for that might be... face[0] = points[0][x,y,z]; face[4] = points[4][x,y,z]; etc... I think this STARTS to handle it... but not sure what to do next \((?=\d) // any parenthesis that is followed by a digit... But not I need to pipe in the space. do I use a pipe like in cmdl unix? \((?=\d|\s|?=\d)^ Something like that? Am I even in the right ballpark? Here is a link to the HTML part of the code. http://061375.com/html5/canvas_3dwireframe_simplecube.html Thanks for any help in advance. Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/ Share on other sites More sharing options...
ragax Posted January 28, 2012 Share Posted January 28, 2012 Hi knot13yet, From your question, I wasn't clear if you were trying to extract the numbers from one specific line, or from the whole block of lines you posted. Let me know, in the meantime here is code to extract your data from one specific line. Input: 4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) ) ) Code: <?php $regex=',(?x) (?(DEFINE)(?<Cap>\((?>[^)]+\)))) (?>(?:-?\d\s){4}\([ ]) ((?&Cap))\s((?&Cap))\s((?&Cap))\s((?&Cap)),'; $string='4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) )'; if(preg_match($regex,$string,$match)) { echo $match[2].'<br />'; echo $match[3].'<br />'; echo $match[4].'<br />'; echo $match[5].'<br />'; } ?> Output: (0 0) (4 4) (6 6) (2 2) I thought this would be a great chance to demo the DEFINE feature from my post yesterday about two little-known php regex features. Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312027 Share on other sites More sharing options...
knot13yet Posted January 28, 2012 Author Share Posted January 28, 2012 Thank you. I will read your post on DEFINE... Although from what I see so far it might as well be in Chinese I will probably be a regular poster here n the near future. REGEXP are one area I absolutely need to learn, but as yet, still allude me. Thanks Again!!!! Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312028 Share on other sites More sharing options...
knot13yet Posted January 28, 2012 Author Share Posted January 28, 2012 BTW will this handle larger numbers? (100 200) etc... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312030 Share on other sites More sharing options...
ragax Posted January 28, 2012 Share Posted January 28, 2012 REGEXP are one area I absolutely need to learn, but as yet, still allude me. You won't regret it, they're heaps of fun. BTW will this handle larger numbers? (100 200) etc... Yes. (Try it! Just change $string in the code I sent you.) Basically, we defined a pattern called "Cap". We capture that pattern four times. Cap is whatever is present within these four sets of parentheses, including the parentheses themselves: \((?>[^)]+\) So is this solved? Meaning, are you happy with the version that extracts the data from one-line input? Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312032 Share on other sites More sharing options...
knot13yet Posted January 28, 2012 Author Share Posted January 28, 2012 Just Tested Works perfect !!! :D :D Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312034 Share on other sites More sharing options...
ragax Posted January 28, 2012 Share Posted January 28, 2012 Sweet, great to hear. Wishing you a relaxing weekend. Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312035 Share on other sites More sharing options...
knot13yet Posted January 28, 2012 Author Share Posted January 28, 2012 function get3Dfaces($data) { $result = array(); foreach($data['faces'] as $key => $value) { if(0 != $key) { $value = ltrim($value); $value = rtrim($value); $string=$value; $face_length = substr($string,0,strpos($string,' ')); $regex=',(?x) (?(DEFINE)(?<Cap>\((?>[^)]+\)))) (?>(?:-?\d\s){4}\([ ]) '; for($i=0; $i<$face_length; $i++) { $regex.='((?&Cap))\s'; } $regex.=','; if(preg_match($regex,$string,$match)) { $result[] = $match; } } } return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/255945-3-sets-of-digits-between-parethesis/#findComment-1312042 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.