How to delete characters that are repeated more than twice with the word R?

I want to delete characters that are repeated more than twice. for instance

 "hhaaappppyyyyyyy mmoooooorning friendsssssssssssssss, good goood day"

to

 "hhaappyy mmoorning friendss, good good day"

. I tried something similar, but it does not reduce exactly to 2 repetitions.

gsub('([[:alpha:]])\\1{2}', '\\1', 
   'hhaaappppyyyyyyy mmoooooorning friendsssssssssssssss, good goood day')

#[1] "hhappyyy mmoorning friendsssss, good god day"

Thank.

+4
source share
2 answers

You need to use a quantifier {2,}and use two \1in the replacement:

s<-'hhaaappppyyyyyyy mmoooooorning friendsssssssssssssss, good goood day'
gsub('([[:alpha:]])\\1{2,}', '\\1\\1', s)
# => [1] "hhaappyy mmoorning friendss, good good day"

See a demonstration of the R .

The pattern ([[:alpha:]])\\1{2,}matches and captures the letter in group 1, and then 2 or more repetitions of the same char are repeated. Two \1in the replacement pattern replace all matches with 2 occurrences of char. You can use two placeholders \1because each match has at least 3 identical characters.

+6

, Wiktor Stribiżew, javascript (, ), .

var sInput = "hhaaappppyyyyyyy mmoooooorning friendsssssssssssssss, good goood day";
var sOutput = sInput.replace(/(.)\1{2,}/g, "$1$1");
console.log(sOutput);
Hide result
0

Source: https://habr.com/ru/post/1694589/


All Articles