Hello friends, in this Java tutorial we want to learn about Java Regex Search and Replace, so Regular expressions or regex are powerful tools for searching and manipulating text. Java provides builtin package that is called java.util.regex. using this package it will be easy to use regex in your Java applications. in this article we want to talk that how to use Java regex for search and replace operations.
What is Java Regex ?
Regex is a pattern that defines a set of strings. it is a sequence of characters that forms a search pattern that can be used to match, find and manipulate text. Java regex is a part of the Java language and it provides a way to work with regular expressions. Java regex package contains two main classes, Pattern and Matcher. Pattern class represents a compiled regex pattern, and the Matcher class is used to match a pattern against a given input.
Java Regex Search
Java regex search involves finding a specific pattern in a text. for doing regex search in Java, first we need first to compile a pattern using Pattern class. The Pattern class has compile() method that takes a regex pattern as a string and returns a Pattern object.
This is an example of how to compile a regex pattern that matches any word starting with the letter c, in this example the pattern is \bc\w*\b, which matches any word starting with the letter c. backslashes are used to escape the special characters \b and \w.
1 |
Pattern pattern = Pattern.compile("\\bc\\w*\\b"); |
After that you have compiled the pattern, you can create Matcher object and use its find() method to find the first occurrence of the pattern in the input string. find() method returns a boolean value indicating whether a match was found.
This is an example of how to use a Matcher object to search for the pattern in a string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { // Define regex pattern to search for Pattern pattern = Pattern.compile("\\bgeekscoders\\b"); // Define an input string to search in String input = "Welcome to geekscoders.com."; // Create a matcher object and search for the pattern Matcher matcher = pattern.matcher(input); if (matcher.find()) { System.out.println("Match found: " + matcher.group()); } else { System.out.println("Match not found."); } } } |
In the above example we have defined a regex pattern \bgeekscoders\b that matches the word geekscoders. after that we have created a Matcher object and call its find() method to search for the pattern in the input string.
Run the code this will be the result

Java Regex Replace
Java regex replace involves replacing a pattern in a text with new string. for doing regex replace in Java you can use the replaceAll() method of the String class. replaceAll() method takes two arguments, regex pattern to search for, and a replacement string.
This is an example of how to use the replaceAll() method to replace all occurrences of the letter a with the letter x:
1 2 3 4 5 6 7 8 9 10 |
public class Example { public static void main(String[] args) { String input = "The cat in the hat wears a cap."; String output = input.replaceAll("a", "x"); System.out.println(output); } } |
You can also use regex patterns in the replacement string to perform more complex replacements. for example you can use $1 syntax to reference capture groups in the regex pattern.
1 2 3 4 5 6 7 8 9 10 11 |
public class Example { public static void main(String[] args) { String input = "Parwiz Forogh"; String output = input.replaceAll("(\\w+) (\\w+)", "$2, $1"); System.out.println(output); } } |
In the above example regex pattern (\w+) (\w+) matches two words separated by a space. replacement string $2, $1 swaps the order of the words and separates them with a comma.
This will be the result
