Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
regex
string
replace
communitycreator

What is RegExUtils.replacePattern() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

replacePattern() is a static method of the RegExUtils class. It is used to replace all the substrings in the given string that matches the specified regular expression with the DOTALL mode enabled. It will replace the substrings with a specified replacement string.

RegExUtils is defined in the Apache Commons Lang package. To add Apache Commons Lang to the Maven project, add the following dependency to pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

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;

Syntax

public static String replacePattern(final String text, final String regex, final String replacement)

Parameters

  • final String text: The string on which the regex is applied.
  • final Pattern regex: Regular expression.
  • final String replacement: Replacement string.

Return value

The method returns modified text after replacing the substrings with the replacement string that match the regex with the DOTALL mode enabled.

Example

In the code below, we define the text to be a snippet of HTML code with a div tag that has text within it. We use the replacePattern method to replace the tag as well as the text with new lines within it with edpresso.

Output

The output of the code when is executed is as follows:

Welcome edpresso End
import org.apache.commons.lang3.RegExUtils;
public class Main {
public static void main(String[] args)
{
String text = "Welcome <div>\nhello\nthis is educative.io\nhome to learning</div> End";
String pattern = "<.*>";
String replacementString = "edpresso";
System.out.println(RegExUtils.replacePattern(text, pattern, replacementString));
}
}

RELATED TAGS

java
regex
string
replace
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring