To perform find and replace operations using Java regular expressions, you can use the Pattern
and Matcher
classes. Here’s an example code snippet that demonstrates how to do a simple find and replace using regular expressions in Java:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String input = "The quick brown fox jumps over the lazy dog.";
String pattern = "brown";
String replacement = "red";
// Create a Pattern object from the pattern string
Pattern regex = Pattern.compile(pattern);
// Create a Matcher object for the input string
Matcher matcher = regex.matcher(input);
// Replace the matches with the replacement string
String output = matcher.replaceAll(replacement);
// Print the result
System.out.println(output);
}
}
In this example, we have a input string The quick brown fox jumps over the lazy dog.
. We want to replace the word “brown” with “red”. The regular expression Pattern
object is created with the pattern “brown”. The Matcher
object is then created using the input string. Finally, the replaceAll()
method is used to replace all occurrences of the pattern with the replacement string.
When you run this code, the output will be: The quick red fox jumps over the lazy dog.
.
This is just a basic example of using Java regular expressions for find and replace operations. Regular expressions can be as simple or as complex as you need them to be, allowing you to perform powerful pattern matching and manipulation tasks.