All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java String Replace Regex

Last Updated : Mar 11, 2024

Java String Replace Regex

In this article we will show you the solution of java string replace regex, substrings can be replaced within a string using the potent replace() function provided by the Java String class. Patterns called regular expressions are used to match and edit text.

These patterns can be used to execute complex string substitutions in Java using the replace() function with regex.

Regex can be used to find particular letters, words, or even intricate patterns inside a string.

Regex allows you to either replace a pattern everywhere it appears in a string or to replace only particular occurrences of a pattern.

Two parameters are required by the replace() method: the replacement string and the regex pattern to match.

When the pattern is located in the string, the replacement string is used in its place. Now move to the concept of java string replace regex.

Step By Step Guide On Java String Replace Regex :-

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReplaceExample {
    public static void main(String[] args) {
        String inputString = "Welcome to talkerscode! talkerscode is a great website.";
        // Regular expression pattern to match "talkerscode"
        String regex = "\\btalkerscode\\b";
        // Replacement string
        String replacement = "TalkersCode";
        // Create a Pattern object
        Pattern pattern = Pattern.compile(regex);
        // Create a Matcher object
        Matcher matcher = pattern.matcher(inputString);
        // Replace all occurrences of "talkerscode" with "TalkersCode"
        String outputString = matcher.replaceAll(replacement);
        // Print the output string
        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
  1. As you can see, we wrote java code to demonstrate the string replace regex.
  2. The Matcher and Pattern classes are imported from the java.util.regex package.
  3. After that, we create a public class called StringReplaceExample.
  4. We define the main method as the program's entry point inside the StringReplaceExample class.
  5. We declare and initialise a string variable named inputString with the following input string: "Welcome to talkerscode! Talkerscode is an excellent website."
  6. We define a regular pattern of expressions using the string \btalkerscode\b. This pattern will match the term "talkerscode" as a whole word, while the \b boundary markers ensure that partial instances are not matched.
  7. The replacement string "TalkersCode" is declared and initialised in a string variable called replacement. This string will be used to replace any occurrences of "talkerscode" in the input string.
  8. Using the Pattern.compile() function, we build a Pattern object and supply the regular expression regex as an argument.
  9. We build a Matcher object by executing the matcher() method on the pattern object and supplying the inputString as an argument.
  10. Using the pattern, we can use the Matcher object to conduct matching and replacing operations on the input string.
  11. We invoke the Matcher object's replaceAll() method, supplying the replacement string as an input. This function returns the updated string after replacing all instances of the pattern in the input string with the replacement string.
  12. We save the updated string in the outputString variable.
  13. Finally, we use the System.out.println() method to print the input & output strings.

Conclusion :-

As a result, we have mastered the notion of java string replace regex.

We also discovered that complicated string substitutions can be achieved by combining the replace() function in the Java String class with regex patterns.

Regular expressions allow text to be matched and edited, whether it be individual letters, words, or detailed patterns inside the string.

I hope this article on java string replace regex helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪