Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove characters which repeat more than twice in a string [duplicate]

I have this text:

F <- "hhhappy birthhhhhhdayyy"

and I want to remove the repeat characters, I tried this code

https://stackoverflow.com/a/11165145/10718214

and it works, but I need to remove repeat characters if it repeats more than 2, and if it repeated 2 times keep it.

so the output that I expect is

"happy birthday"

any help?

like image 579
Fatima Avatar asked Apr 10 '19 07:04

Fatima


People also ask

How do I check if a string has a repeating character?

If we want to know whether a given string has repeated characters, the simplest way is to use the existing method of finding first occurrence from the end of the string, e.g. lastIndexOf in java. In Python, the equivalence would be rfind method of string type that will look for the last occurrence of the substring.

How do I remove a character from a string?

We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

How do I remove a character from a string in Java?

Although the String class doesn't have a remove() method, you can use variations of the replace() method and the substring() method to remove characters from strings.


1 Answers

Try using sub, with the pattern (.)\\1{2,}:

F <- ("hhhappy birthhhhhhdayyy")
gsub("(.)\\1{2,}", "\\1", F)

[1] "happy birthday"

Explanation of regex:

(.)          match and capture any single character
\\1{2,}      then match the same character two or more times

We replace with just the single matching character. The quantity \\1 represents the first capture group in sub.

like image 148
Tim Biegeleisen Avatar answered Sep 16 '22 11:09

Tim Biegeleisen