I’m trying to write a regex or Ruby method which will find the longest repeated pattern in a string. For example:
"abcabc" => "abc"
"cccc" => "c"
"abcd" => "abcd"
What is the best way to implement this? I naïvely tried /^(.*)*$/ but that won’t work as it just matches the full string.
I knew it couldn’t be that complicated, so I thought it over and found a solution:
This will return the length of the repeated pattern. It finds this by generating rotations of the string.