tags:

views:

165

answers:

3

If there are more than 2 characters "Hiiiiiii My frieeend!!!!!!!"

I need to be reduced to "Hii My frieend!!"

Please undestand that in my language there are many words with double chars. Thnx in advance

kplla

+2  A: 
codaddict
note the OP wants the output "Hii My frieend!!" -- so this isn't quite right yet.
Mark E
I believe the request was to leave doubles but not more than doubles, right?
Devin Ceartas
Thanks Mark and Devin.
codaddict
Yes Devin. No more than doubles. My language is brazillian. (sorry) I forgot to tell you but I need in perl.(sorry again) The idea is to reduce to no more than 2. This way I´m allowing people to express their "feelings" and I´ll not destroy words that have double chars. thnx
kplla
@kplla: while I appreciate the effort to reduce this kind of writing, I have to tell you that you'll be fighting your users here. They **will** find a way to work around your filter (for example "Hii.ii.ii.ii.ii My friee.eend!!.!!") and they will have a lot of endurance in finding ways around such filters.
Joachim Sauer
But they are children (for now) and they will stop as they see something that cuts their idea back. In the future I´ll have to work it better... Thanks.
kplla
@Downvoter: Care to explain ?
codaddict
+10  A: 

Perl / regex (and if it's not english, Perl has given me better luck with Unicode than PHP):

#!/usr/bin/perl

$str = "Hiiiiii My Frieeeeend!!!!!!!";

$str =~ s/(.)\1\1+/$1$1/g;

print $str;
Devin Ceartas
Perfect!!!! Thank you Devin
kplla
\1\1+ is equivalent to \1{2,} (2 or more of the first capture)
kixx
kixx: you're right; that might be cleaner. brian: wow, first time I got the code correct enough and all you had to do was edit my capitalization! I feel proud. ;-)
Devin Ceartas
+1  A: 

Here's another regex solution that uses lookahead (just for fun), in Java:

System.out.println(
    "Hiiiiii My Frieeeeend!!!!!!!".replaceAll("(.)(?=\\1\\1)", "")
); // prints "Hii My Frieend!!"
polygenelubricants