java - How to create a regex that replaces two or more consecutive identical characters with only two? -
assuming have twitter message similar following:
"hoot, soooooo coooool!!!"
i want come java regex string.replaceall result in following:
"hoot, cool!"
i started , have tried permuting following without success:
original.replaceall("(.)\\1+", "$1");
does know how come regex greedily reduce several consecutive characters 2 characters? solution must not reduce 2 repeating characters 1 (e.g. word hoot should not reduce hot).
if need replace 2+ characters two, can modify expression, this:
original.replaceall("(.)\\1+", "$1$1");
however, there not enough information in regex make exception "soooooo"
, trim "so"
, opposed "soo"
.
here demo on ideone.
Comments
Post a Comment