regex - identifying repeating numbers or number patterns in php -
i've seen quite few question on identifying repeating patterns crazy looking strings , stuff nothing capture repeating number or repeating number pattern.
i'm trying figure out way write function can identify both of these cases. example have number pattern similar 14285714285714 pattern being 142857-142857-14. in cases pattern say, 7575757 : 75-75-75-7. have reoccurring number 55555555 or 55555556.
how go creating function determines if number either repeating or has pattern? guess repeating number seen pattern in sense. i'm kind of @ loss on , on appreciated.
thank in advance.
edit need throw true if pattern or re occurrence longer 3 digits.
update tried @stribizhev recommendation preg_match , indeed able detect pattern. still need pattern more precise though. if number 4444 preg_match shows pattern 44-44. need able know difference in 4-4-4-4 , 75-75-75. can 1 me clarify how more precise result preg_match?
here's have far.
$num = 4444; if (count($num) >= 3) { $result = preg_match('/(\d+)\1/', $num, $matches); if ($result) { $repeat = "true"; echo "match: ".$matches[0].", ".$matches[1]; } } output: match: 4444, 44 although output isn't inaccurate, it's not specific need be. 44 pattern, more 4 pattern. in 7575, 75 pattern.
this pattern job:
$pattern = '~ \a # start of string # find largest pattern first in lookahead # (the idea compare size of trailing digits smallest pattern) (?= (\d+) \1+ (\d*) \z ) # find smallest pattern (?<pattern> \d+? ) \3+ # has same or less trailing digits (?! .+ \2 \z) # capture eventual trailing digits (?= (?<trailing> \d* ) ) ~x'; if (preg_match($pattern, $num, $m)) echo 'repeated part: ' . $m[0] . php_eol . 'pattern: ' . $m['pattern'] . php_eol . 'trailing digits: ' . $m['trailing'] . php_eol;
Comments
Post a Comment