Java Regex 2 - Duplicate Words

  • + 0 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.