JChilds Posted November 7, 2008 Share Posted November 7, 2008 I know this shoudl be simple, but regex scares me, and I don't use it often enough to learn it. I need to find all occurances of: (1-3 numbers)|(1-3 numbers) Returned in an array. eg 324|432 12|223 1|1 123|8 Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/ Share on other sites More sharing options...
DarkWater Posted November 7, 2008 Share Posted November 7, 2008 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 https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684943 Share on other sites More sharing options...
JChilds Posted November 7, 2008 Author Share Posted November 7, 2008 That is excellent! Thank you very much. I will look at your tutorial right now. Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684946 Share on other sites More sharing options...
JChilds Posted November 8, 2008 Author Share Posted November 8, 2008 When I use print_r on the above example, it only prints out 324|432. None of the other matches. Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684969 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 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 https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684970 Share on other sites More sharing options...
JChilds Posted November 8, 2008 Author Share Posted November 8, 2008 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 https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684976 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 I did something similar to this for a Facebook programming puzzle. Can you just show me an example of input and desired output? Do you want to find, for each point given, the closest other point, or what? Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684978 Share on other sites More sharing options...
JChilds Posted November 8, 2008 Author Share Posted November 8, 2008 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 https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684983 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 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 https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684988 Share on other sites More sharing options...
JChilds Posted November 8, 2008 Author Share Posted November 8, 2008 Thats right. Which ever point gets there first, gets priority. Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684993 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 Oh. Err, I'll work on it I guess. You might need to fill in some parts if I'm feeling lazy. Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684994 Share on other sites More sharing options...
JChilds Posted November 8, 2008 Author Share Posted November 8, 2008 Excellent. Thank you so much! Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-684998 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 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 https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-685001 Share on other sites More sharing options...
JChilds Posted November 8, 2008 Author Share Posted November 8, 2008 Ok thanks. Talk about bad timing though, A mate of mine has jsut turned up so I guess I'll be away from the PC for a few hours. Link to comment https://forums.phpfreaks.com/topic/131853-simple-regex-problem/#findComment-685011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.