Codementor Events

Java Regular Expression: part 6 - Replacing text

Published Jan 19, 2019Last updated Apr 05, 2019
Java Regular Expression: part 6 - Replacing text

There are many cases that we need to replace a string or certain characters. For instance, we need to replace error or redundant characters with the right ones.
The java.lang.String class in Java supports 2 methods for these purposes: replace() and replaceAll()
For instance, I have a string as follows:

“This is a strIng wIth some typos”

In the string, there are some upper case I letters, and I want to replace them with lower case i letter.
The code:

public class Demo {
    public static void main(String[] args) {
        String text = " This is a strIng wIth some typos ";
        text = text.replace("I", "i");
        System.out.println(text);
    }
}

In the above code, I invoked the replace() method in the String class:

text = text.replace("I", "i");

The replace() method takes 2 parameters:

  • the first is the character or substring you want to replace
  • the second is the new character or substring

The replace() method will replace all occurrences of the old character or substring with the new one. And in our example code, the method will replace all the upper case I characters with the lower case i characters
Let’s run the program and see the result:

This is a string with some typos

However, the replace() method does not take regular expression as its first parameter.
Therefore, it will not work in cases we want to replace certain characters based on a particular pattern.
For instance, if we have a string like the following:

“This is 34a string 23that contain56s many typos”

In the above string, we want to remove all the digits. And it is much more convenient if we could specify \d as pattern to be removed.
Of course, we can use the replace() method to achieve the task, but then we need to specify individual characters as a replacement.
We have a better solution: the replaceAll() method.
The replaceAll() method works similarly to the replace() counterpart with one significant difference: it supports regular expression as its first parameter.
Let’s see the code:

public class Demo {
    public static void main(String[] args) {
        String text = "This is 34a string 23that contain56s many typos";
        text = text.replaceAll("\\d", "");
        System.out.println(text);
    }
}

In the above code, I have invoked the String.replaceAll() method as follows:

text = text.replaceAll("\\d", "");
  • The first parameter should be \d because we want to remove all the digits in the input string.
  • The second parameter is the new string to be replaced. Since we want to remove digits, an empty character should be appled here.

Let’s run the program and we’ll see all the digits are gone:

This is a string that contains many typos

Let’s take another.

String s2 = "This is te$xt wi%th s\*ome spe!cial characters in in";
        String nonePrintableCharsPattern = "[^\\w ]";
        String newString2 = s2.replaceAll(nonePrintableCharsPattern, "");
        System.out.println(newString2);

I have another text containing some unintended special characters: the dollar sign, the percentage sign, the star character, and the exclamation mark. Those special characters need to be removed.
Since there are many special characters, it is easier to specify in the patterns what we want to keep, rather than what we want to remove.
So in the pattern, I use the caret character (^) followed by \w and a whitespace. That pattern means: remove everything except those represented by w and the whitespace.
Why the whitespace? Because the \w character represents only for upper and lower case characters from a to z and digits from 0 to 9, not including the whitespace. So if we don’t specify the whitespace in our pattern, all whitespaces will be removed.
Run the program and you’ll see the output:

This is text with some special characters in it

Previous part

Next part

--

Visit learnbyproject.net for a free Regular Expression courses and other free courses

Discover and read more posts from Sera.Ng
get started