For this word, I want to look for all the substrings that appear next to each other at least 3 times, and replace all of them with only one. I know how to do this when a substring is just one character. For example, the code below returns "Bah" for the input string "Bahhhhhhh":
String term = "Bahhhhhhh"; term = term.replaceAll("(.)\\1{2,}", "$1");
However, I need a more general template that turns Bahahahaha into Baha.
source share