如何在没有给定模式的情况下将字符串拆分为相同的部分?

如何在没有给定模式的情况下将字符串拆分为相同的部分?

问题描述:

I have a function that returns every time a different string but with a specific random pattern, this pattern could be as simple as "a, b, c, a, b, c" or something much more complicated.

So what I need is to write a function that searches in the string for a pattern and return it.

There is only one condition to be considered, let's say we have this string for example: "a, b, a, b, c, a, b, a, b, c"

In that string you can't say that "a, b" is a pattern, to consider a specific string as a pattern then it should be longer than the remaining string, in that case, "a,b" is (2 digits * 2) = 4 and the remaining is "c, a, b, a, b, c" 6 digits, so what consider a real pattern is "a, b, a, b, c".

I was about writing a function to do that but I know it's going to be a complicated one so I thought to ask SO before if maybe there is a built-in functionality in PHP or Javascript which can do something close from that or makes the job easier for me, so any ideas guys?

我有一个函数,每次返回一个不同的字符串,但具有特定的随机模式,这个模式可以很简单 作为“a,b,c, a,b,c strong>”或更复杂的东西。 p>

所以我需要的是编写一个搜索的函数 模式的字符串并将其返回。 p>

只有一个条件需要考虑,假设我们有这个字符串例如:“a,b,a,b,c,a ,b,a,b,c“ p>

在该字符串中,您不能说”a,b“是一种模式,要将特定字符串视为模式,那么它应该是 比剩下的字符串长,在这种情况下,“a,b”是(2位* 2)= 4,剩下的是“c,a,b,a,b,c”6位数,所以考虑一个真实的模式 是“a,b,a,b,c”。 p>

我是在编写一个函数来做到这一点,但我知道这将是一个复杂的函数,所以我想先问一下 如果可能有PHP或Javascript中的内置功能可以这样做 从中接近或者让我的工作变得更轻松,所以任何想法的人都会这样做吗? p> div>

You can do this with a regexp: ^([a-z]{2,})(?:\1)+$. The regex uses a recursive pattern to match any string that has a repeating pattern, placing the pattern in the first group. This is how to use it in PHP:

Edit

The regex has been updated to allow an incomplete pattern on the end (as long as there has been one repeat) and then that is checked that it matches for its length against the pattern, to allow matching strings such as abcabca.

function check_match($string) {
    if (preg_match('/^([a-z]{2,})(?:\1)+(.*)$/', $string, $matches)) {
        return strlen($matches[2]) == 0 || $matches[2] == substr($matches[1], 0, strlen($matches[2])) ? $matches[1] : false;
    }
    return false;

}

foreach (array("abca", "abcabcab", "abcabcabcabc", "ababcababc", "aabbaabb", "aabaab", "aabaabd", "abcd", "aabcaabc", "abcabca") as $str) {
    if ($pattern = check_match($str))
        echo "$str matched $pattern
";
    else 
        echo "no pattern for $str
";
}

Output:

no pattern for abca
abcabcab matched abc
abcabcabcabc matched abcabc
ababcababc matched ababc
aabbaabb matched aabb
aabaab matched aab
no pattern for aabaabd
no pattern for abcd
aabcaabc matched aabc
abcabca matched abc

Sounds like the simplest solution would be to start from the beginning of the string and find the smallest substring starting from index 0 such that repeating that substring some number of times results in the input string:

const findPattern = str => {
  const { length } = str;
  for (let i = 1; i < str.length - 1; i++) {
    const testPattern = str.slice(0, i);
    const repeats = Math.ceil(length / i);
    if (str === testPattern.repeat(repeats).slice(0, length)) return testPattern;
  }
  return 'No pattern found';
}
console.log(findPattern('ababcababc'));
console.log(findPattern('aabbaabb'));
console.log(findPattern('aabaab'));
console.log(findPattern('aabaabaab'));
console.log(findPattern('abcdabc'));
console.log(findPattern('abcd'));

</div>