Java Regex 2 - Duplicate Words

  • + 0 comments

    what wrong in this code ? public static void main(String[] args) {

         Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());;
        String regex = "\\b(\\w+)(?:\\s+\\1\\b)+";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        while (n > 0) {
            String input = scanner.nextLine();
            Matcher matcher = pattern.matcher(input);
            String resultTmp = matcher.replaceAll("$1");
            System.out.println(resultTmp);
            n--;
        }
        scanner.close();
    }