3

How should one use preg_replace() to replace a string from 'aabbaacc' to 'abc'?

Currently, my code uses str_split() then array_unique() then implode(). I think preg_replace() can achieve this also, but I don't know how.

Thank you for your help.

thomaux
  • 19,133
  • 10
  • 76
  • 103
user995789
  • 279
  • 1
  • 4
  • 15
  • FWIW, I like your method. – Jonathan M Mar 08 '13 at 13:50
  • 2
    possible dup: http://stackoverflow.com/questions/6723389/remove-repeating-character – Nick Fury Mar 08 '13 at 13:56
  • @NickFury Actually, now that I think about this, that regex will only catch duplicates in sequence. Not if they appear later in the string. As a result, `aabbaacc` would become `abac` instead of `abc`, I believe. – Colin M Mar 08 '13 at 13:58
  • thanks nick, the method in your post will return 'abac', but not 'abc', – user995789 Mar 08 '13 at 13:59
  • Ignore that possible dup as @ColinMorelli pointed out, it's not the same question. – Nick Fury Mar 08 '13 at 14:03
  • @ColinMorelli i may not be an expert, but i at least know a little about programming. I benchmarked both the "3 loops" code and your function, and it turns out that the "3 loops" is about 3x faster than your function. Sorry to disapoint you though :) – HamZa Mar 08 '13 at 14:27
  • @HamZaDzCyberDeV Because you ran it with the string in question here (and also probably don't know how to benchmark). Which is an _extremely_ unlikely scenario. I benchmarked the same thing using a long string, which is considerably more likely to be used in a real world application. You may look at the code [here](http://sandbox.onlinephpfunctions.com/code/f5dec0188d383742dfc46a9222bd221f86a3ccd7) and run it your self. My method (the first) is clearly faster. Benchmarking unrealistic inputs is a complete waste of everyone's time. Go ahead, feel free to change the string and test again – Colin M Mar 08 '13 at 14:30
  • @ColinMorelli weird ... I saw the results online and yes your function is faster, but when i try it on my localhost the 3loops is about x3 faster even when i put some other long string o.O – HamZa Mar 08 '13 at 14:38

3 Answers3

2

A regex that seems to work for me is /(.)(?=.*?\1)/. Please test it for yourself here:

http://regexpal.com/

I've also tested it with preg_replace('/(.)(?=.*?\1)/', '', 'aaabbbabc') which returns the expected abc.

Hope this helps :)

Jaap Haagmans
  • 6,232
  • 1
  • 25
  • 30
  • Would it be possible to keep the order of appearance of the charactere? Using the provided regex: `bbbaaacccabc` => `abc` What if I want `bac`? – cooltea Mar 08 '13 at 15:06
  • Sorry, I overlooked that. It takes the last occurrence of every character, not the first one. The suggestion behind your link is okay, I can't think of a way to do that with only regular expressions. – Jaap Haagmans Mar 11 '13 at 14:31
0

try this

$string = 'dbbaabbbaac';

$new = preg_replace_callback( array("/(.)\\1+/"),function($M){print_r($M);return $M[1];}, $string  );
$new = preg_replace_callback( array('/(.)(.?\\1)/i','/(.)(.*?\\1)/i'),function($M){return $M[1].trim($M[2],$M[1]);}, $new  );
echo $new."\n";

output

dbac

or this with out Regex

$value="aabbaacc";
for($i=0;$i<strlen($value);$i++){
    $out[$value[$i]]=$value[$i];
}
echo implode("",$out);

output:

abc
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
0

This is the closest I got. However, it's basically a copy of : How do I remove duplicate characters and keep the unique one only in Perl?

<?php
    $string = 'aabbaacc';

    $new = preg_replace( '/(.)(?=.*?\1)/i','', $string  );

    echo $new;
?>

Unfortunately, it does not keep the string in the same order. I don't know if that is important to you or not.

Community
  • 1
  • 1
Justin Noel
  • 5,945
  • 10
  • 44
  • 59