Java Regex 2 - Duplicate Words

  • + 0 comments

    something is wrong, not able to output anything import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        String regex = "\\b(\\w+)\\b(?:\\s+\\b\\1\\b)+";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Scanner scan = new Scanner(System.in);
        int count = scan.nextInt();
        scan.nextLine();
        for(int i = 0; i< count; i++){
            String s = scan.nextLine();
            System.out.println(s);
            String smallCase = s.toLowerCase();
            Matcher matches = p.matches(smallCase);
            String result = matches.replaceAll("$1");
            System.out.println(result);
            }
        }
    

    }