Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to replace repeated characters

Tags:

java

regex

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.

like image 847
Rajith Shanika Avatar asked Feb 10 '23 07:02

Rajith Shanika


2 Answers

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

like image 189
Avinash Raj Avatar answered Feb 11 '23 21:02

Avinash Raj


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.

like image 24
G_hi3 Avatar answered Feb 11 '23 19:02

G_hi3