Rajith Shanika
Rajith Shanika

Reputation: 161

Regex to replace repeated characters

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.

Upvotes: 4

Views: 6323

Answers (2)

G_hi3
G_hi3

Reputation: 598

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.

Upvotes: 2

Avinash Raj
Avinash Raj

Reputation: 174874

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

Upvotes: 8

Related Questions