Jump to content

Simple Regex problem


JChilds

Recommended Posts

1) There's a regex forum.  Just letting you know for next time.

 

2) Try this:

<?php
$input = <<<STRING
324|432
12|223
1|1
123|8
STRING;

preg_match_all('(\d{3}\|\d{3})', $input, $matches);
print_r($matches);
?>

 

3) I assumed you wanted the array to be like:

Array
(
    [0] => Array
        (
            [0] => 324|432
        )

)

If you wanted:

Array
(
    [0] => Array
        (
            [0] => 324
        )

    [1] => Array
        (
            [0] => 432
        )

)

 

Just let me know.

 

4) Read my regex tutorial on the home page of this site if you need a refresher on regex.

Link to comment
Share on other sites

Yeah, I know.  I wasn't sure how you wanted it to be formatted at the end.  Try this then:

 

<?php
$input = <<<STRING
324|432
12|223
1|1
123|8
STRING;

preg_match_all('(\d{3})\|(\d{3})', $input, $matches);
print_r($matches);
?>

 

$matches[0] contains left side numbers and $matches[1] contains right side ones.

Link to comment
Share on other sites

These are points on a graph.

I plan to have two input text fields.

The user enters a number of points into each text field. (in the format 345|987 or 234|43 or 1|1 etc)

The actual application of this involves a copy/paste of a page with hundreds of these in amoung other random text)

 

After the form is submitted, they are shown which points are clostest together.(and how far apart they are)

No two points can be used more than once.

 

So I was thinking, get the first result in $first_points_field and using the distance formula on all points in $second_points_field.

After the smallest distance is found, remove those entries from $first_points_field and $second_points_field.

 

I hope I am amking sense.

Link to comment
Share on other sites

Ok here is an example input.

 

$input1 =

plot 337 (431|82)

your own 0092160

2450035000 Commands

plot 346 (429|75)

your own 00108630

2043034900 Commands

plot 353 (413|77)

your own 00105990

2000035000 Commands

 

$input2=

plot 337 (112|124)

your own 0092160

4572475050 Commands

plot 346 (134|75)

your own 00108630

4572472700 Commands

plot 353 (433|145)

your own 00105990

572700 Commands

 

There will only be 3 outputs in this case.

Link to comment
Share on other sites

OH.  I misread your first post!  The regex should have been:

 

<?php
$input = <<<STRING
324|432
12|223
1|1
123|8
STRING;

preg_match_all('/(\d{1,3})\|(\d{1,3})/', $input, $matches);
print_r($matches);
?>

 

Now, let me make sure the logic I'm thinking of is correct:

 

Parse inputs into arrays

Loop through first input array

    For each in second array, do distance formula, sort resulting array, and take the closest one

    Remove element from first array

End loop for first array

 

Correct?  Or do you need to remove the point from both the first and second arrays?  What if two plots both share the same point as their closest point?

Link to comment
Share on other sites

And the laziness strikes. D:  Also, I need to go do my homework.  I started it for you though:

 

<?php
//get your inputs however you want, I don't know.  I'm assuming $input1 and $input2

preg_match_all('/\b\d{1,3}\|\d{1,3}\b/', $input1, $data_one);
preg_match_all('/\b\d{1,3}\|\d{1,3}\b/', $input2, $data_two);

function get_distance($other, $curr) {

$dist = sqrt(pow($curr[0] - $other[0], 2) + pow($curr[1] - $other[1], 2));
return $dist;
}

function split_to_array($array) {
return explode("|", $array);
}

$one = array_map('split_to_array', $data_one[0]);
$two = array_map('split_to_array', $data_two[0]);

while ($curr = array_pop($one)) {
//process
}

 

The distance formula is there for you, and some basic processing.  I might be able to come back later and finish this up (and possibly fix some speed issues that I can foresee happening >_<), but I don't know.

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.