Regular expression – how to delete duplicate characters, but leave two?

If there are more than 2 characters
“Hiiiiiii
My friend!!!!!!!”

I need to reduce to
“Fei Yichuan
My friend!!”

Please note that in my language, there are many double-character words.
Thnx in advance

kplla

Perl / regex (if it’s not in English, Perl gives me better than Unicode Luck):

#!/usr/bin/perl

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

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

print $str;

If there are more than 2 characters
“Hiiiiiii
My friend!!!!!!!”

I need to reduce to
” Fei Chuan
My friend!!”

Please note that in my language, there are many double-character words.
Thnx in advance

kplla

Perl / regex (If it is not in English, Perl gave me better luck than Unicode):

#!/usr/bin/perl

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

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

print $str;

Leave a Comment

Your email address will not be published.