Reputation: 11
but it remove white space. I don't want to remove white space! What should I do?
Original text
I like like like this movie, it was so so so good good good.
Remove duplicates
I likelike this movie, it was soso goodgood.
Here is my code:
String result = s.replaceAll("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+", "$1$1");
Upvotes: 0
Views: 869
Reputation: 23225
As Dawood ibn Kareem points out in the comments, if you want a space between words, why don't you add a space in your replacement pattern, like so:
String result = s.replaceAll("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+", "$1 $1");
Upvotes: 1
Reputation: 3553
Updated Answer
Sorry for posting a second answer, but I just want to ascertain that you get notified of this answer.
String h = s.replaceAll("(?i)\\b([a-z]+)\\b((?:\\s+\\1\\b)+)", "$2");
h = h.replaceAll("\\s+", " "));
This should remove the last repeated word :)
Upvotes: 0
Reputation:
Try this.
String result = s.replaceAll("(?i)(\\b\\w+\\b)\\s+\\1", "$1");
Upvotes: 0
Reputation: 152
/**
*
* @author Md. Amran Hossain
*/
public class DuplicateWordRemove {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String s = "like like like";
// TODO code application logic here
String result = s.replaceAll("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+", "$1");
System.out.println(""+result);
}
}
Like
Upvotes: 0
Reputation: 3553
Your code is correct, but you just need to replace $1$1
with $1
.
EDIT: It is also mentioned in the comments, by Dawood ibn Kareem.
Upvotes: 0