PHP and Regex - Replacing Repeating Characters with Single Characters in a String

[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.


Site submission links:

Stumble! Add to Mixx!

Related Posts:

7 Responses to “PHP and Regex - Replacing Repeating Characters with Single Characters in a String”

  1. webdev.andrei Says:

    Still not satisfied. Although you used Regular expressions there is a loop as I have a loop. But your loop executes two RegExp whilst mine executes only simple comparison and a simple push to an array. Everyone can see very easily that not using RegExp to accomplish this will be faster and easier.

    I want to mention again that “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.

  2. admin Says:

    Ahhh… I understand a bit now what you were looking for.

    I didn’t realize that you were looking to have a single regular expression do all the replacements necessary. I just assumed you were looking for a way to do the same actions using regex instead of the loop/array structure you created.

    But now I know… and knowing is half the battle.

  3. TLP Says:

    Here’s a better method using a single regular expression thanks to backreferences.

    $str = ‘aabbccaaaaaddee’;
    echo preg_replace(’{(.)\1+}’,'$1′,$str);
    //abcade

    I converted Andrei’s version from Actionscript to PHP to benchmark it with the other 2 versions. I timed each doing 10,000 runs.

    Admin - 1.226 seconds
    Andrei - 0.492 seconds
    Mine - 0.113 seconds

  4. webdev.andrei Says:

    TLP thanks but is NOT what I needed. The citation is taken out of the context and you couldn’t fully understand my problem which is explained here: http://www.flexer.info/2008/03/11/remove-duplicate-chars-from-a-string/

    So what I needed is a string like “aabbccaaaaaddee” to be transformed to “abcde“.

    I tried with regular expression but there is no easy way (one step).

    Thanks for benchmarks. Great job. If you could remake your solution to accomplish the transformation (even with more steps) and do the benchmark again it will be great.

  5. webdev.andrei Says:

    To explain it even better… so what I needed is a string like “aabbccaaaaaddee” to be transformed to “abcde” (please note that there are two groups of “b>a“s in the first string and only one “a” in the resulting group).

    Using “aabbccaaaaaddee” in your regular expression will give us the result “abcade” which is different from “abcde” (note the underlined letters). I need unique chars in the resulting string.

  6. webdev.andrei Says:

    It seems that in the comment is not showing the underlines. Please see the comment also http://www.flexer.info/2008/03/11/remove-duplicate-chars-from-a-string/#comment-173

  7. admin Says:

    Thanks for the discussion and the help TLP. I updated the post to reflect your better version.

Leave a Reply