We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This code works, but if you remove the comments, it doen't work anymore!
importjava.util.Scanner;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassDuplicateWords{publicstaticvoidmain(String[]args){Stringregex="\\b(\\w+)(\\b\\W+\\b\\1\\b)*";Patternp=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);Scannerin=newScanner(System.in);intnumSentences=Integer.parseInt(in.nextLine());while(numSentences-->0){Stringinput=in.nextLine();Matcherm=p.matcher(input);// Check for subsequences of input that match the compiled patternwhile(m.find()){input=input.replaceAll(m.group(0),m.group(1));}// Prints the modified sentence.System.out.println(input);}in.close();}}
Java Regex 2 - Duplicate Words
You are viewing a single comment's thread. Return to all comments →
This code works, but if you remove the comments, it doen't work anymore!