You are viewing a single comment's thread. Return to all comments →
String regex = "\b(\w+)(\b\W+\b\1\b)*"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); input = input.replaceAll("\b" + Pattern.quote(m.group()) + "\b", m.group(1));
The Pattern.quote method quotes part of a regex pattern to make regex interpret it as string literals.
Seems like cookies are disabled on this browser, please enable them to open this website
Java Regex 2 - Duplicate Words
You are viewing a single comment's thread. Return to all comments →
String regex = "\b(\w+)(\b\W+\b\1\b)*"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); input = input.replaceAll("\b" + Pattern.quote(m.group()) + "\b", m.group(1));
The Pattern.quote method quotes part of a regex pattern to make regex interpret it as string literals.