Given a string str which represents a sentence, the task is to remove the duplicate words from sentences using regular expression in java.
Examples:
Input: str = “Good bye bye world world”
Output: Good bye world
Explanation:
We remove the second occurrence of bye and world from Good bye bye world worldInput: str = “Ram went went to to to his home”
Output: Ram went to his home
Explanation:
We remove the second occurrence of went and the second and third occurrences of to from Ram went went to to to his home.Input: str = “Hello hello world world”
Output: Hello world
Explanation:
We remove the second occurrence of hello and world from Hello hello world world.
Approach
- Get the sentence.
- Form a regular expression to remove duplicate words from sentences.
regex = "\\b(\\w+)(?:\\W+\\1\\b)+";
The details of the above regular expression can be understood as:
- “\\b”: A word boundary. Boundaries are needed for special cases. For example, in “My thesis is great”, “is” wont be matched twice.
- “\\w+” A word character: [a-zA-Z_0-9]
- “\\W+”: A non-word character: [^\w]
- “\\1”: Matches whatever was matched in the 1st group of parentheses, which in this case is the (\w+)
- “+”: Match whatever it’s placed after 1 or more times
- Match the sentence with the Regex. In Java, this can be done using Pattern.matcher().
- return the modified sentence.
Below is the implementation of the above approach:
// Java program to remove duplicate words // using Regular Expression or ReGex. import java.util.regex.Matcher; import java.util.regex.Pattern; class GFG { // Function to validate the sentence // and remove the duplicate words public static String removeDuplicateWords(String input) { // Regex to matching repeated words. String regex = "\\b(\\w+)(?:\\W+\\1\\b)+"; Pattern p = Pattern.compile( regex, Pattern.CASE_INSENSITIVE); // Pattern class contains matcher() method // to find matching between given sentence // and regular expression. 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)); } return input; } // Driver code public static void main(String args[]) { // Test Case: 1 String str1 = "Good bye bye world world"; System.out.println( removeDuplicateWords(str1)); // Test Case: 2 String str2 = "Ram went went to to his home"; System.out.println( removeDuplicateWords(str2)); // Test Case: 3 String str3 = "Hello hello world world"; System.out.println( removeDuplicateWords(str3)); } } |
Good bye world Ram went to his home Hello world
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find all the numbers in a string using regular expression in Python
- How to validate identifier using Regular Expression in Java
- How to validate time in 12-hour format using Regular Expression
- Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
- Validating Roman Numerals Using Regular expression
- How to validate time in 24-hour format using Regular Expression
- How to validate pin code of India using Regular Expression
- How to check Aadhar number is valid or not using Regular Expression
- How to validate Visa Card number using Regular Expression
- How to validate Indian Passport number using Regular Expression
- How to validate PAN Card number using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- How to check string is alphanumeric or not using Regular Expression
- Finding Data Type of User Input using Regular Expression in Java
- How to validate GST (Goods and Services Tax) number using Regular Expression
- How to validate HTML tag using Regular Expression
- How to validate a domain name using Regular Expression
- How to validate image file extension using Regular Expression
- How to validate IFSC Code using Regular Expression
- How to validate CVV number using Regular Expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

