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.
Java Regex 2 - Duplicate Words
Java Regex 2 - Duplicate Words
Sort by
recency
|
389 Discussions
|
Please Login in order to post a comment
With this code I got the same expected results except for a mysterious character at the end of the last test case. It is neither a whitespace or unprintable char. It makes no sense for me.
String regex = "\b(\w+)(\s+\1\b)+"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
my solution: 1. \b - is a word delimiter 2. \w+ - any word, letter, digit or underscore 3. \s+ - blank spaces 4. \1 - back reference (anything captured by (\w+))
Is this what we really want? I think that this paragraph should be improved
3. Write the two necessary arguments for replaceAll such** that each repeated word is replaced with the very first instance the word found in the sentence**. It must be the exact first occurrence of the word, as the expected output is case-sensitive.
public static void main(String[] args) {