removeAll
is a static method of the RegExUtils
class that is used to remove all the substrings in a given string that matches the specified regular expression. Here, removing means replacing the substring with an empty string.
RegExUtils
is defined in the Apache Commons Lang
package. To add the Apache Commons Lang
to the Maven project, add the following dependency to the pom.xml file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Note: For other versions of the commons-lang package, refer to the Maven Repository.
RegExUtils
class can be imported as follows.
import org.apache.commons.lang3.RegExUtils;
public static String removeAll(final String text, final Pattern regex)
final String text
: The string on which the regex is applied.final Pattern regex
: The regular expression.The return value is the modified text after the substrings that match the regex are removed.
In the code below, we define a text that has educative
repeated multiple times in the text. Next, we remove the word educative
from the text using the removeAll
method. The output of the code when executed would be the text without the word educative
.
import org.apache.commons.lang3.RegExUtils;import java.util.regex.Pattern;public class Main {public static void main(String[] args){String text = "educative is the best platform for java. educative is the best platform for python. educative is the best platform for c.";System.out.println(RegExUtils.removeAll(text, Pattern.compile("educative")));}}