Re: [PHP] PHP script for detecting pattern sequences?
sorry, should have added that i'm not aware of any library to do this, but you could certainly write one! :) and i forgot to use the list, sorry. - isaac On Fri, Jul 10, 2009 at 10:28 AM, Isaac Dover isaacdo...@gmail.com wrote: though this looks suspiciously like a homework assignment, i'll bite. those regex patterns wouldn't solve his problem. he wants to pull repetitions from the string _before_ knowing a pattern. those patterns will match the entire source string without trying, i would think that you may try using a technique that reads a character, then looks for the next occurrence of that character. if you're lucky, then that character will be the beginning of the sequence. you'll just look at the substring from that first occurrence until the next, then search the string for that substring. if unlucky, you'll move to the next string, _append it_ to the previous, repeat the process, and so on. at some point, you'll have the pattern built in memory and will be searching the source string using your built pattern string. at some point, something will have to match. the trick is in recursion. also, i'm assuming your real examples are more complicated than what you have above. in the two listed, you already know that a zero indicates that the pattern is beginning, so you just look for, and note the index of, zeroes. i've thumbed through a free book online that deals with text parsing. it's very technical, but interesting at the same time. maybe you can find it. - isaac On Wed, Jul 8, 2009 at 11:32 PM, WenDong Zhang zwd2...@gmail.com wrote: yes (\d+?)\1+ works fine On Thu, Jul 9, 2009 at 6:00 AM, Per Jessen p...@computer.org wrote: Rob Gould wrote: Can anyone tell me if there's a PHP library out there that will help me determine pattern sequences from a string? Example input: 032258064516129032258064516129032258064516129032258064516129 Sequence = 032258064516129 037037037037037037037037037037037037037037037037037037037037 Sequence = 037 I know regex can help you find a pattern when you know what the pattern is already, but this is different. Nah, it's the same thing. A suitable regex might look something like this: /([0-9]+)\1+/ Not tested, probably won't work on the first try. You may need greediness adjustments. /Per -- Per Jessen, Zürich (14.1°C) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Best Regards! Wen Dong
Re: [PHP] PHP script for detecting pattern sequences?
On Fri, Jul 10, 2009 at 10:30 AM, Isaac Doverisaacdo...@gmail.com wrote: On Wed, Jul 8, 2009 at 11:32 PM, WenDong Zhang zwd2...@gmail.com wrote: On Thu, Jul 9, 2009 at 6:00 AM, Per Jessen p...@computer.org wrote: A suitable regex might look something like this: /([0-9]+)\1+/ Not tested, probably won't work on the first try. You may need greediness adjustments. /Per yes (\d+?)\1+ works fine -- Best Regards! Wen Dong those regex patterns wouldn't solve his problem. he wants to pull repetitions from the string _before_ knowing a pattern. those patterns will match the entire source string - isaac Those patterns look like a pretty good starting point to me. True, the first captured result of preg_match would be the entire string, but the submatches array would contain the actual sequence that is repeated: ?php $pattern = '/(\d+?)\1+/'; $subject = '032258064516129032258064516129032258064516129032258064516129'; if (preg_match($pattern, $subject, $matches)) { var_dump($matches); } /* array(2) { [0]= string(60) 032258064516129032258064516129032258064516129032258064516129 [1]= string(15) 032258064516129 } */ $subject = '037037037037037037037037037037037037037037037037037037037037'; if (preg_match($pattern, $subject, $matches)) { var_dump($matches); } /* array(2) { [0]= string(60) 037037037037037037037037037037037037037037037037037037037037 [1]= string(3) 037 } */ $subject = ''; if (preg_match($pattern, $subject, $matches)) { var_dump($matches); } /* array(2) { [0]= string(60) [1]= string(1) 3 } */ ? Some slight adjustments to the pattern could also be useful. // This would catch a pattern of any repeating characters, not just numeric digits $pattern = '/(.+?)\1+/'; // This would only match if the entire string was a repeated sequence $pattern = '/^(\d+?)\1+$/'; // This would match the repeated sequence only if the string began with a repeated sequence. $pattern = '/^(\d+?)\1+/'; // This would match the repeated sequence only if the string ended with a repeated sequence. $pattern = '/(\d+?)\1+$/'; If a string had multiple sequences, you could also use preg_match_all to find each sequence, but that looks a bit more involved than the OP. None of these require knowing the sequence in advance. How do they not satisfy the OP? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP script for detecting pattern sequences?
i just got pwned! thanks, andrew. i should've paid more attention to what i was reading. - isaac On Fri, Jul 10, 2009 at 11:19 AM, Andrew Ballard aball...@gmail.com wrote: On Fri, Jul 10, 2009 at 10:30 AM, Isaac Doverisaacdo...@gmail.com wrote: On Wed, Jul 8, 2009 at 11:32 PM, WenDong Zhang zwd2...@gmail.com wrote: On Thu, Jul 9, 2009 at 6:00 AM, Per Jessen p...@computer.org wrote: A suitable regex might look something like this: /([0-9]+)\1+/ Not tested, probably won't work on the first try. You may need greediness adjustments. /Per yes (\d+?)\1+ works fine -- Best Regards! Wen Dong those regex patterns wouldn't solve his problem. he wants to pull repetitions from the string _before_ knowing a pattern. those patterns will match the entire source string - isaac Those patterns look like a pretty good starting point to me. True, the first captured result of preg_match would be the entire string, but the submatches array would contain the actual sequence that is repeated: ?php $pattern = '/(\d+?)\1+/'; $subject = '032258064516129032258064516129032258064516129032258064516129'; if (preg_match($pattern, $subject, $matches)) { var_dump($matches); } /* array(2) { [0]= string(60) 032258064516129032258064516129032258064516129032258064516129 [1]= string(15) 032258064516129 } */ $subject = '037037037037037037037037037037037037037037037037037037037037'; if (preg_match($pattern, $subject, $matches)) { var_dump($matches); } /* array(2) { [0]= string(60) 037037037037037037037037037037037037037037037037037037037037 [1]= string(3) 037 } */ $subject = ''; if (preg_match($pattern, $subject, $matches)) { var_dump($matches); } /* array(2) { [0]= string(60) [1]= string(1) 3 } */ ? Some slight adjustments to the pattern could also be useful. // This would catch a pattern of any repeating characters, not just numeric digits $pattern = '/(.+?)\1+/'; // This would only match if the entire string was a repeated sequence $pattern = '/^(\d+?)\1+$/'; // This would match the repeated sequence only if the string began with a repeated sequence. $pattern = '/^(\d+?)\1+/'; // This would match the repeated sequence only if the string ended with a repeated sequence. $pattern = '/(\d+?)\1+$/'; If a string had multiple sequences, you could also use preg_match_all to find each sequence, but that looks a bit more involved than the OP. None of these require knowing the sequence in advance. How do they not satisfy the OP? Andrew
[PHP] PHP script for detecting pattern sequences?
Can anyone tell me if there's a PHP library out there that will help me determine pattern sequences from a string? Example input: 032258064516129032258064516129032258064516129032258064516129 Sequence = 032258064516129 037037037037037037037037037037037037037037037037037037037037 Sequence = 037 I know regex can help you find a pattern when you know what the pattern is already, but this is different. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP script for detecting pattern sequences?
Rob Gould wrote: Can anyone tell me if there's a PHP library out there that will help me determine pattern sequences from a string? Example input: 032258064516129032258064516129032258064516129032258064516129 Sequence = 032258064516129 037037037037037037037037037037037037037037037037037037037037 Sequence = 037 I know regex can help you find a pattern when you know what the pattern is already, but this is different. Nah, it's the same thing. A suitable regex might look something like this: /([0-9]+)\1+/ Not tested, probably won't work on the first try. You may need greediness adjustments. /Per -- Per Jessen, Zürich (14.1°C) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP script for detecting pattern sequences?
yes (\d+?)\1+ works fine On Thu, Jul 9, 2009 at 6:00 AM, Per Jessen p...@computer.org wrote: Rob Gould wrote: Can anyone tell me if there's a PHP library out there that will help me determine pattern sequences from a string? Example input: 032258064516129032258064516129032258064516129032258064516129 Sequence = 032258064516129 037037037037037037037037037037037037037037037037037037037037 Sequence = 037 I know regex can help you find a pattern when you know what the pattern is already, but this is different. Nah, it's the same thing. A suitable regex might look something like this: /([0-9]+)\1+/ Not tested, probably won't work on the first try. You may need greediness adjustments. /Per -- Per Jessen, Zürich (14.1°C) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Best Regards! Wen Dong