3

Suppose I have a string like :

      String s = "hellllooooo howwwwwww areeeeeee youuuuuuu";

I want to discard the repeated letters and want to get :

     "helloo howw aree youu"

I have done the matching using ::

        matches(".*([a-z])\\1{3,}.*"

But how can I replace the helloooooooo to helloo and the others ?

2
  • If I may ask: I haven't seen "\\1" in a regexp pattern before, what does it do? Commented Mar 5, 2013 at 0:54
  • how about using 'for'? trace from index 0-s.length() ? Commented Mar 5, 2013 at 0:57

1 Answer 1

4

Any of the following produces the result you want:

s = s.replaceAll("([a-z])\\1+", "$1$1");

s = s.replaceAll("(([a-z])\\2)\\2*", "$1");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.