Replace or Remove All Digits of a String in Java using regular expression

Tags: String Pattern regex

In this article we are going to learn how to use core Java classes to remove all numeric characters from a String or replace numeric characters with other characters. We will show you different Java code examples how to use String.replaceAll() method and java.util.regex.Pattern class.

Java code example using regex “[0-9]” with String.replaceAll() method

public class RemoveReplaceDigitsExample1 {
    public static void main(String[] args) {
        String testString = "Your OTP (one time password) is 112233";
        // Remove all numeric characters
        String removedDigitsString = testString.replaceAll("[0-9]", "");

        // Replace all numeric characters with "*"
        String replacedDigitsString = testString.replaceAll("[0-9]", "*");

        System.out.println(removedDigitsString);
        System.out.println(replacedDigitsString);
    }
}
Output:

Your OTP (one time password) is 
Your OTP (one time password) is ******

Java code example using regex “\d” with String.replaceAll() method

public class RemoveReplaceDigitsExample2 {
    public static void main(String[] args) {
        String testString = "Your OTP (one time password) is 112233";
        // Remove all numeric characters
        String removedDigitsString = testString.replaceAll("\\d", "");

        // Replace all numeric characters with "*"
        String replacedDigitsString = testString.replaceAll("\\d", "*");

        System.out.println(removedDigitsString);
        System.out.println(replacedDigitsString);
    }
}
Output:

Your OTP (one time password) is 
Your OTP (one time password) is ******

Java code example using regex “[0-9]” with java.util.regex.Pattern

import java.util.regex.Pattern;

public class RemoveReplaceDigitsExample3 {
    public static void main(String[] args) {
        String testString = "Your OTP (one time password) is 112233";
        Pattern notDigitsPattern = Pattern.compile("[0-9]");

        // Remove all numeric characters
        String removedDigitsString = notDigitsPattern.matcher(testString).replaceAll("");

        // Replace all numeric characters with "*"
        String replacedDigitsString = notDigitsPattern.matcher(testString).replaceAll("*");

        System.out.println(removedDigitsString);
        System.out.println(replacedDigitsString);
    }
}
Output:

Your OTP (one time password) is 
Your OTP (one time password) is ******

Java code example using regex “\d” with java.util.regex.Pattern

import java.util.regex.Pattern;

public class RemoveReplaceDigitsExample4 {
    public static void main(String[] args) {
        String testString = "Your OTP (one time password) is 112233";
        Pattern notDigitsPattern = Pattern.compile("\\d");

        // Remove all numeric characters
        String removedDigitsString = notDigitsPattern.matcher(testString).replaceAll("");

        // Replace all numeric characters with "*"
        String replacedDigitsString = notDigitsPattern.matcher(testString).replaceAll("*");

        System.out.println(removedDigitsString);
        System.out.println(replacedDigitsString);
    }
}
Output:

Your OTP (one time password) is 
Your OTP (one time password) is ******

Happy Coding 😊