3
import java.util.Scanner;

public class redigetajs {
    public static void main(String args[]){

        Scanner scan= new Scanner(System.in);
        System.out.println("Input text!");
        String teksts= scan.nextLine();
        System.out.println("text after change!");
        System.out.println(teksts.replaceAll("(.)\\1{1,}", "$1"));
    }
}
mahesh
  • 1,311
  • 1
  • 13
  • 26
  • 1
    `{1,}` is the same as `+`. [Check the SO regex reference](http://stackoverflow.com/q/22937618) – HamZa May 06 '14 at 11:35

3 Answers3

3

It matches sequences of identical characters and collapses them into only one occurrence.

Example: "aaaaabcccdd" -> "abcd"

cadrian
  • 7,332
  • 2
  • 33
  • 42
1

Suexpression duplicate a group character and removed:

  • (.) - group, also referenced as $1
  • \\1 - subexpression followed it
  • {1,} - to repeat one or more times
Roman C
  • 49,761
  • 33
  • 66
  • 176
1

Perfect tool for this:

http://regex101.com/r/kF3uB7

need 30 characters :)

Jan
  • 1,042
  • 8
  • 22