Try it yourself with our free Regex Tester tool — runs entirely in your browser, no signup needed.

How to Use regex to replace in Java

How to use regex to replace in Java

Regular expressions (regex) are a powerful tool for text processing, and Java provides a robust API for working with them. One of the most common use cases for regex is replacing text patterns in a string. In this article, we'll explore how to use regex to replace text in Java, covering the basics, handling edge cases, common mistakes, and performance tips.

Quick Example

Here's a minimal example that replaces all occurrences of a word with another word:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexReplaceExample {
    public static void main(String[] args) {
        String input = "Hello world, world is beautiful.";
        String pattern = "world";
        String replacement = "earth";

        Pattern regex = Pattern.compile(pattern);
        Matcher matcher = regex.matcher(input);
        String result = matcher.replaceAll(replacement);

        System.out.println(result); // Output: "Hello earth, earth is beautiful."
    }
}

This example uses the java.util.regex package, which is included in the Java Standard Edition. No additional dependencies are required.

Step-by-Step Breakdown

Let's walk through the code line by line:

  1. import java.util.regex.Matcher;: We import the Matcher class, which performs the actual replacement.
  2. import java.util.regex.Pattern;: We import the Pattern class, which compiles the regex pattern.
  3. String input = "Hello world, world is beautiful.";: We define the input string that we want to modify.
  4. String pattern = "world";: We define the regex pattern that we want to match. In this case, it's a simple word.
  5. String replacement = "earth";: We define the replacement string.
  6. Pattern regex = Pattern.compile(pattern);: We compile the regex pattern into a Pattern object. This step is optional, but it improves performance if you're using the same pattern multiple times.
  7. Matcher matcher = regex.matcher(input);: We create a Matcher object that will perform the replacement on the input string.
  8. String result = matcher.replaceAll(replacement);: We use the replaceAll method to replace all occurrences of the pattern with the replacement string.
  9. System.out.println(result);: We print the modified string.

Handling Edge Cases

Here are some common edge cases to consider:

Empty/null input

If the input string is null or empty, the replaceAll method will return an empty string.

String input = null;
String result = matcher.replaceAll(replacement);
System.out.println(result); // Output: ""

Invalid input

If the input string is not a valid regex pattern, the compile method will throw a PatternSyntaxException.

String pattern = "[";
try {
    Pattern regex = Pattern.compile(pattern);
} catch (PatternSyntaxException e) {
    System.out.println("Invalid pattern");
}

Large input

For very large input strings, you may want to consider using a more efficient algorithm or streaming the input.

String input = Files.readString(Paths.get("large_file.txt"));

Unicode/special characters

Regex patterns can match Unicode characters and special characters. Use the Pattern.UNICODE_CHARACTER_CLASS flag to enable Unicode character classes.

String pattern = "é"; // matches the Unicode character é
Pattern regex = Pattern.compile(pattern, Pattern.UNICODE_CHARACTER_CLASS);

Common Mistakes

Here are three common mistakes developers make when using regex to replace in Java:

1. Not compiling the pattern

// Wrong
Matcher matcher = Pattern.matcher(input, pattern);
// Correct
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(input);

2. Not handling null input

// Wrong
String result = matcher.replaceAll(replacement);
// Correct
if (input != null) {
    String result = matcher.replaceAll(replacement);
}

3. Not using the correct flags

// Wrong
Pattern regex = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
// Correct
Pattern regex = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

Performance Tips

Here are two practical performance tips for using regex to replace in Java:

1. Compile the pattern only once

If you're using the same pattern multiple times, compile it only once to improve performance.

Pattern regex = Pattern.compile(pattern);
// use the regex object multiple times

2. Use the replaceAll method instead of replaceFirst

The replaceAll method is generally faster than replaceFirst because it uses a more efficient algorithm.

// Fast
String result = matcher.replaceAll(replacement);
// Slow
String result = matcher.replaceFirst(replacement);

FAQ

Q: What is the difference between replaceAll and replaceFirst?

A: replaceAll replaces all occurrences of the pattern, while replaceFirst replaces only the first occurrence.

Q: How do I match Unicode characters in my regex pattern?

A: Use the Pattern.UNICODE_CHARACTER_CLASS flag to enable Unicode character classes.

Q: Can I use regex to replace text in a file?

A: Yes, you can use the Files class to read and write files, and then use the replaceAll method to replace text.

Q: How do I handle null input when using regex to replace?

A: Check for null input before calling the replaceAll method, and handle it accordingly.

Q: What is the best way to improve performance when using regex to replace?

A: Compile the pattern only once, and use the replaceAll method instead of replaceFirst.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp