Created
July 19, 2020 18:32
-
-
Save cyrexcyborg/c25c0be1060e1251b67743023f1b3c9c to your computer and use it in GitHub Desktop.
Find duplicate words RegExp
This file contains hidden or 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; | |
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