Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
substring
replace
regex
communitycreator

What is RegExUtils.replaceFirst 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.

What is the replaceFirst method?

replaceFirst is a staticAny static member can be accessed before any objects of its class are created and without reference to any object. method of RegExUtils that is used to replace the first substring in a given string that matches the specified regular expression with the specified replacement string.

Add the Apache Common Lang package

RegExUtils is defined in the Apache Common Lang package. To add the Apache Common 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>

For other versions of the commons-lang package, refer to the Maven Repository.

Import RegExUtils in Java

You can import the RegExUtils class as follows.

import org.apache.commons.lang3.RegExUtils;

Syntax


public static String replaceFirst(final String text, final Pattern 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

replaceFirst returns the modified text after only removing the first substring that matches the regex with the replacement string.

Code

In the code below, we define a text with educative repeated multiple times.

Next, we use the replaceFirst method to replace only the first educative from the text with the replacement string edpresso.

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.replaceFirst(text, Pattern.compile("educative"), "edpresso"));
}
}

Output

The output of the code is as follows.


edpresso is the best platform for java. educative is the best platform for python. educative is the best platform for c.

RELATED TAGS

java
substring
replace
regex
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