Jump to content

3 sets of digits between parethesis


knot13yet

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

 

:D

Link to comment
Share on other sites

Thank you.

I will read your post on DEFINE... Although from what I see so far it might as well be in Chinese ;D

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!!!!

 

 

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

 

  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;

}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.