Skip to content

Instantly share code, notes, and snippets.

@cyrexcyborg
Created July 19, 2020 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyrexcyborg/c25c0be1060e1251b67743023f1b3c9c to your computer and use it in GitHub Desktop.
Save cyrexcyborg/c25c0be1060e1251b67743023f1b3c9c to your computer and use it in GitHub Desktop.
Find duplicate words RegExp
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DuplicateWords {
/*
https://rubular.com/r/98MZ0vzUeX
\b : Any word boundary (\w+) : Select any word character (letter, number, underscore)
( : Grouping starts
\b : Any word boundary
\W+ : Any non-word character
\1 : Select repeated words
\b : Un select if it repeated word is joined with another word
) : Grouping ends
*/
public static void main(String[] args) {
String regex = "\\b(\\w+)(\\b\\W+\\1\\b)*";
Pattern p = Pattern.compile(regex, Pattern.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);
// Check for subsequences of input that match the compiled pattern
while (m.find()) {
input = input.replaceAll(m.group(), m.group(1));
}
// Prints the modified sentence.
System.out.println(input);
}
in.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment