Can someone give me a Java regex to replace the following.
If I have a word like this "Cooooool", I need to convert this to "Coool" with 3 o's. So that I can distinguish it with the normal word "cool".
Another ex: "happyyyyyy" should be "happyyy"
replaceAll("(.)\\1+","$1"))
I tried this but it removes all the repeating characters leaving only one.
Change your regex like below.
string.replaceAll("((.)\\2{2})\\2+","$1");
(
start of the first caturing group.(.)
captures any character. For this case, you may use [a-z]
\\2
refers the second capturing group. \\2{2}
which must be repeated exactly two times.)
End of first capturing group. So this would capture the first three repeating characters.\\2+
repeats the second group one or more times.DEMO
I think you might want something like this:
str.replaceAll("([a-zA-Z])\\1\\1+", "$1$1$1");
This will match where a character is repeated 3 or more times and will replace it with the same character, three times.
$1
only matches one character, because you're surrounding the character to match.\\1\\1+
matches the character only, if it occurs at least three times in a row.
This call is also a lot more readable, than having a huge regex and only using one $1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With