The Matcher class is commonly used in conjunction with Pattern. Use the Matcher.replaceAll() method to replace all matches in the string

String str = "This is a great day...";
Pattern p = Pattern.compile("\\bis\\b", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
String result = m.replaceAll("<h1>is</h1>");

Note: Using the \b regex command will match on a word boundary (like whitespace). This is helpful to use in order to ensure that only the word "is" is matched and not words that contain the letters "i" and "s" (like "island").

Answer from Michael on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › java string.replaceall()
Java.String.replaceAll() | Baeldung
July 29, 2026 - Sometimes, when we have these characters in our regex pattern, we want the regex engine to treat them as literal characters. We have two options to achieve that: escaping the character using backslash or putting it in a character class. Next, let’s solve the problem in a previous example: replacing all “.” characters with “:“: String input = "hi.java.hi.world"; String result = input.replaceAll("\\.", ":"); assertEquals("hi:java:hi:world", result); result = input.replaceAll("[.]", ":"); assertEquals("hi:java:hi:world", result);
🌐
Coderanch
coderanch.com › t › 381432 › java › regex-replace-substring
use regex to replace a substring (Java in General forum at Coderanch)
November 14, 2006 - Date = "10/20/2001" And you don't want to use a common new Value to replace all of them. You could use the find() method of the Matcher class (with a properly defined regex) along with other methods in the Matcher class, such as group(), to get the matches, and then programmatically determine the replacement value, and then replace the match with that value.
🌐
DeepSource
deepsource.com › directory › java › issues › JAVA-P1001
Use `String.replace()` instead of `String.replaceAll()` for simple text patterns (JAVA-P1001) ・ Java
JAVA-P1001 · Major · Performance · String.replaceAll() is a method that accepts a regex string, and replaces all occurrences of the regex with the provided replacement string. If you are only trying to replace a specific substring and not ...
🌐
W3Schools
w3schools.com › java › ref_string_replaceall.asp
Java String replaceAll() Method
In the returned string, instances of $n will be replaced with the substring that was matched by the group or, if $0 is used, by the whole expression. See "More Examples" below for an example of using a backreference. Tip: See the Java RegEx tutorial to learn about regular expressions.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-replace-a-string-using-regex-pattern-in-java
How to Replace a String Using Regex Pattern in Java? - GeeksforGeeks
July 23, 2025 - The program defines a regex pattern to match digits (\\d+) and a replacement string ("number"). It then uses replaceAll() to replace all occurrences of digits in the input string with the replacement string.
🌐
Baeldung
baeldung.com › home › java › java string › how to use regular expressions to replace tokens in strings in java
How to Use Regular Expressions to Replace Tokens | Baeldung
September 13, 2025 - Pattern regexCharacters = Pattern.compile("[<(\\[{\\\\^\\-=$!|\\]})?*+.>]"); assertThat(replaceTokens("A regex character like [", regexCharacters, match -> "\\" + match.group())) .isEqualTo("A regex character like \\["); For each match, we prefix the \ character. As \ is a special character in Java strings, it’s escaped with another \. Indeed, this example is covered in extra \ characters as the character class in the pattern for regexCharacters has to quote many of the special characters.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 21, 2026 - java.util.regex · java.lang.Object · java.util.regex.Pattern · All Implemented Interfaces: Serializable · public final class Pattern extends Object implements Serializable · A compiled representation of a regular expression. A regular expression, specified as a string, must first be compiled into an instance of this class.
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
Regular expressions can be used to perform all types of text search and text replace operations. Java does not have a built-in Regular Expression class, but we can import the java.util.regex package to work with regular expressions.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string replace() method
Java String replace() Method
December 24, 2024 - The Java String replaceAll() method replaces EVERY single occurrence of a regular expression passed as a parameter with the desired String. This means, that every copy of regex is updated by the replacement string.
🌐
JetBrains
jetbrains.com › help › idea › tutorial-finding-and-replacing-text-using-regular-expressions.html
Find and replace text using regular expressions | IntelliJ IDEA Documentation
3 weeks ago - Enter a search string in the top field and a replace string in the bottom field. Click to enable regular expressions. If you want to check the syntax of regular expressions, hover over and click the Show expressions help link. When you search for a text string that contains special regex symbols, IntelliJ IDEA automatically escapes them with backlash \ in the search field.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-replaceall-method-explained-ec87fae6d15e
Medium
August 22, 2024 - In this simple example, the substring “World” is replaced with “Java”. However, the real power of replaceAll() shines when using regular expressions. Regular expressions, often abbreviated as regex, are a powerful and flexible way to search, match, and manipulate strings based on specific ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - Pattern.compile(regex).matcher(str).replaceAll(repl) Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.
🌐
Medium
medium.com › @AlexanderObregon › replacing-all-digits-in-a-string-with-java-regex-9970edf56308
Replacing All Digits in a String with Java Regex | Medium
September 16, 2025 - The regex pattern is compiled into a Pattern object, which is then paired with a Matcher. As the matcher moves through the string, it locates every section that matches the pattern and marks those spots for replacement. Because strings in Java never change after they’re built, the original sequence is left untouched.
🌐
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
🌐
Medium
medium.com › @AlexanderObregon › javas-string-replace-method-explained-aa35534af671
Java’s String.replace() Explained | Medium
October 7, 2024 - This example demonstrates how you can use replace() to remove excess whitespace in a string, making the text more readable and consistent. To identify the number of spaces used by a user at runtime in a web application, you can count the occurrences of spaces in a string by comparing the length of the original string with the length of the string after removing the spaces. This can be done using Java’s replace() method, which allows you to replace characters or substrings in a string.
🌐
Udemy
blog.udemy.com › home › it & development › software development › understanding the java string replace method
Understanding the Java String Replace Method - Udemy Blog
April 14, 2026 - In the above format, the “regEx” string object is a regular expression that is used to search the string and replaces all matched substrings with the ‘replacement’. This Java String ‘replace’ method in addition to a NullPointerException exception throws a PatternSyntaxException ...
🌐
Udemy
blog.udemy.com › home › it & development › software development › java string replaceall: how to replace certain elements of a string
Java String ReplaceAll: How to Replace Certain Elements of a String - Udemy Blog
April 14, 2026 - To learn more about strings and character arrays in Java, you can take this course. The replaceAll() method is used to replace all the occurrences of a regular expression or substring, within a larger string, with a new string.
🌐
Java Mex
javamex.com › tutorials › regular_expressions › search_replace_loop.shtml
Search and replace with Java regular expressions: using Matcher.find()
How to perform search and replace operations on a string using Matcher.find() and regular expressions in Java.
🌐
TutorialsPoint
tutorialspoint.com › article › replace-one-string-with-another-string-with-java-regular-expressions
Replace one string with another string with Java Regular Expressions
Let us see a program to replace one string with another string using Java Regular Expressions − · public class Example { public static void main( String args[] ) { String str = new String("Good Harry Good"); System.out.println( "Initial String : "+ str); // replacing "Good" with "Bad" str = str.replaceAll( "Good" , "Bad" ); System.out.println( "The String after substitution : "+str ); } }