Last active
June 19, 2018 17:03
-
-
Save tuxerrante/b87bf543cef0e4fc4d155a2cd71ccb27 to your computer and use it in GitHub Desktop.
java exercise to use regex in order to delete repeated words, case insensitive, in a sentence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import static java.util.regex.Pattern.CASE_INSENSITIVE; | |
import java.util.ArrayList; | |
public class DuplicateWords { | |
public static void main(String[] args) { | |
/* | |
https://stackoverflow.com/a/37143875/3673430 | |
\\b([a-zA-Z]+)(\\b\\s\\1)+ takes also word parts | |
(\\b[a-zA-Z]+\\b\\s?)(\\b\\1\\b)+ doesn't work with words at the end of line | |
\\b([a-zA-Z]+)(\\b\\s\\b\\1\\b)+ works | |
\\b([a-z]+)(\\s\\b\\1\\b)+ more efficient | |
*/ | |
String regex = "\\b([a-z]+)(\\s\\b\\1\\b)+"; | |
Pattern p = Pattern.compile(regex, CASE_INSENSITIVE); | |
Scanner in = new Scanner(System.in); | |
int numSentences = Integer.parseInt(in.nextLine()); | |
while (numSentences-- > 0) { | |
String input = in.nextLine(); | |
Matcher m = p.matcher(input); | |
while (m.find()) { | |
input = input.replaceAll(m.group(), m.group(1)); | |
} | |
System.out.println(input); | |
} | |
in.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment