CorvusPrime Posted May 9, 2012 Share Posted May 9, 2012 I am looking for a preg_match expression that will find recurring numeric patterns in a space delimited list of 1-2 digit numbers. ex: 12 5 6 7 8 7 7 8 7 3 21 and regex should find 787 as the first repeating pattern. New to regex and trying to get my head around this so I can do it lucidly in the future. BTW: I have tried the following: /[0-9]{1,2} [0-9]{1,2} [0-9]{1,2}/\1+ And what I get is an error about unknown modifier in reference to the final \1+ Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 what do you consider a repeated pattern? 1 2 3 1 2 4 is "1 2" considered a repeated pattern? Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Thanks for jumping in. The answer is three as in "1 2 3" is a pattern. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 now wait a minute, that's a different pattern.. "1 2 3" is *a* pattern in that it is sequence of numbers with the same incremented value, but "1 2 3" is not the same pattern definition as "7 8 7" in your example. In my example, "1 2 3" is not repeated... Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 I misread your example. Specifically we are looking for repeating patterns in a string that consists of numbers of up tp two digits and spaces. A second example would be a string such as: 1 12 3 4 12 3 4 5 6 2 3 6 2 3 4 1 In the above string the number sequence "12 3 4" is repeated once and is the first repeating pattern. That is what I am trying to return with the regex. The first pattern of three space delimited numbers that are repeated. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 okay so back to my original question, are TWO numbers considered a repeated pattern, or does it have to be a minimum of 3? Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Not two. A sequence of three repeating numbers. ex: 12 3 4 Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 3 or more or exactly 3? I hope you are seeing a theme to my questions here...be more specific in what you want Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Agreed. Let's do three or more. And sorry...dealing with VERY vague reqs on my end. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 Okay, one more question...do the numbers have to be next to each other? eg: 1 2 3 1 2 3 vs. 1 2 3 4 1 2 3 Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Adjacency is not a requirement. And we only need to return the first match found. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 $string = "12 5 6 12 5 7 8 7 5 7 8 7 3 21 12 5 6"; preg_match('~((\d+\s?){3,}).*\1~',$string,$matches); $number = $matches[1]; Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Thanks so much!! Trying it against this string: 2 0 7 1 1 7 1 1 6 8 7 and it is returning no matches on 7 1 1. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 hmm...then you must have more to the string than you've mentioned, or else you have some other code not working properly. $string = "2 0 7 1 1 7 1 1 6 8 7"; preg_match('~((\d+\s?){3,}).*\1~',$string,$matches); $number = $matches[1]; echo $number; that echo's "7 1 1" Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Had an offset param ;o) Fixed now. Thanks for this! I am going to study this and get the regex thing "on board". Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 oooh actually I found a bug in my pattern...apparently it will match partial numbers. For example: 112 5 6 12 5 7 8 7 5 7 8 7 3 21 12 5 60 Pattern will match "12 5 6" that's no bueno... Here is an updated pattern that makes use of lookarounds to make sure it matches whole numbers... $string = "2 0 7 1 1 7 1 1 6 8 7"; preg_match('~(((?<!\d)\d+\s?){3,}).*(?<!\d)\1(?!\d)~',$string,$matches); $number = $matches[1]; Quote Link to comment Share on other sites More sharing options...
CorvusPrime Posted May 9, 2012 Author Share Posted May 9, 2012 Gotcha. Interesting! I tried a few patterns on my own and definitely appreciate the one you sent over. By far the simplest implementation. Part of the madness that comes with regex noobs...over-complication-itis Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.