Java String replaceAll

In this basic tutorial of Java, we will look at the Java String replaceAll method with String class. The Java String replaceAll() method returns a String after it replaces each substring based on the match with a given regular expression. Let’s see this in more details with some examples:


1. String replaceAll() Method

Before we start, let’s take a quick look at the method signature of replaceAll() method:

* Replaces each substring of this string that matches the regular expression with the
* given replacement.
public String replaceAll(String regex, String replacement) {
   return Pattern.compile(regex).matcher(this).replaceAll(replacement);

Keep in mind that the method will throw PatternSyntaxException if the regular expression’s syntax is invalid.Let’s take an example to understand this:

public class PatternSyntaxExceptionExample {
    public static void main(String[] args) {

        String regex = "["; // invalid regular expression
        String errorStr= "Java Dev Journal example".replaceAll(regex, "error");

If we run this code, we will see the following error/ exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
	at java.util.regex.Pattern.error(
	at java.util.regex.Pattern.clazz(
	at java.util.regex.Pattern.sequence(
	at java.util.regex.Pattern.expr(
	at java.util.regex.Pattern.compile(
	at java.util.regex.Pattern.(
	at java.util.regex.Pattern.compile(
	at java.lang.String.replaceAll(
	at com.javadevjournal.PatternSyntaxExceptionExample.main(

Let’s take some example for the replaceAll() method


1.1. Replace all matching String occurances

Let’s take an example where we want to replace all occurrences of String with another string. We can accomplish this easily with replaceAll() method.

public class StringReplaceAllExample {

    public static void main(String[] args) {

        String input = "While working on the development environment, all system points to development sandbox";

        String finalOutput= input.replaceAll("development", "production");


Let’s take another example where we like to replace all the white spaces from a String.We can do that by passing the "\\s" as the regex pattern.

String output = inputStr.replaceAll("\\s", "");


2. String replaceFirst() Method

In case we only want to replace the first substring, we can use the replaceFirst() method

public class StringReplaceFirstExample {

    public static void main(String[] args) {

        String input = "While working on the development environment, all system points to development sandbox";

        String finalOutput= input.replaceFirst("development", "production");


Output for the above program:

While working on the production environment, all system points to development sandbox



In this post we saw how to use Java String replaceAll method. We saw how to use this method to replace a String based on the regular expression. At the end of the post, we saw how to use the replaceFirst() method to replace the first String occurrence.

0 0 vote
Article Rating

Manish Sharma

Manish's primary interests are Java, Spring Boot and Spring. His focus is more toward the automations and testing.Manish love travelling and when not working, he might be exploring some new destination.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x