I saw this as an answer for finding repeated words in a string. But when I use it, it thinks This and is are the same and deletes the is
The below pattern will match duplicate words even with any number of occurrences.
Pattern.compile("\\b(\\w+)(\\b\\W+\\b\\1\\b)*", Pattern.MULTILINE+Pattern.CASE_INSENSITIVE);
For e-g, "This is is my my my pal pal pal pal pal pal pal pal" will output "This is my pal"
Also, Only one iteration with "while (m.find())" is enough with this pattern.