[Update] – a comment left by TLP gives a much better solution to the problem that seems to be better in benchmarks as well. I’m changing the post to reflect the best and fastest method for the situation described.
In a previous post, I described a situation where we needed to remove a repeating dot in a user name. In this article, I mentioned the first site that came up when searching in Google to find a solution.
I thought that I might have come across something that would be a solution for what this person was looking for. However, they clarified what they needed the function to do on their website and left me a comment with some more info:
“Thanks for citing us but the article was about making a unique chars string. For example: “aabbccaaaaaddee” will become “abcade“. That is what I accomplished in the article. I know very well regular expression and there are some way of accomplishing that but not in one step. Every char of the string must be separated by a separator (ex: comma, pipe, etc) and then apply the lookback regular expression.
The beauty of using regular expressions is this: a lot of steps that are needed to accomplish some string formating/parsing can be done in one step using RegExp. But if there are the same amount of steps it will be faster not to use RegExp.”
I love programming and I definitely love a challenge and so, webdev.andrei: I accept your challenge.
$str = 'aabbccaaaaaddee'; echo preg_replace('{(.)\1+}','$1',$str); //abcade |
This type of simple expression is thanks to a commenter that dropped by named TLP. We’ll try break it down to see how it works. The curly braces {(.)\1+} without a number in the middle means to repeat it as many times as needed until it no longer occurs. The round brackets {(.)\1+} create something called a backreference. A backreference allows it to reuse part of the regular expression match in the expression itself. So, when it comes to the first character repeating more than once, it will replace it with a single version of itself (or $1). It then places itself back in at the location of the backslash-1 {(.)\1+}.
Throughout the night until the wee hours of the morning I was furthering my ‘regex-fu’. I ultimately came to a simple loop that seems to satisfy the issue.
$string = 'aabbccaaaaaddee'; $new_string = ''; $starting_char = 0; while (strlen($string) > 0 && $starting_char < strlen($string)) { $blah = preg_match('/[A-z]{2,}/', $string, $matches); $letter = $matches[0][$starting_char]; $new_string .= $letter; $regex = '/' . $letter . '{2,}/'; $string = preg_replace($regex, $letter, $string); $starting_char++; } echo $new_string; |
In short: it tries to find a repeating character. When it finds one, it replaces it with a single version of itself. Now that it knows the first repeating instance is now a single character in the string, it can move on to the next character of the string and try that one out. It does this until there are only single instances of each character left in the string.
And there you have it. This could be done with any number of different characters by altering the first line of the loop and inputting a different range of characters instead of [A-z].
I hope this helps others and especially hope that webdev.andrei can get some use out of it.
If you liked this post, then please be sure to subscribe to my feed.
Pingback: استفسار بخصوص تكرار الأحرف ! - سوالف سوفت()