Try this:

String input = "Hello cat Cats cats Dog dogs dog fox foxs Foxs";
input = input.replaceAll("(?i)\\s*(?:fox|dog|cat)s?", "");

Demo

Answer from Tim Biegeleisen on Stack Overflow
🌐
W3Schools
w3schools.com › JAVA › ref_string_replaceall.asp
Java String replaceAll() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... String myStr = "I love cats. Cats are very easy to love. Cats are very popular."; String regex = "(?i)cat"; System.out.println(myStr.replaceAll(regex, "dog"));
🌐
Baeldung
baeldung.com › home › java › java string › java string.replaceall()
Java.String.replaceAll() | Baeldung
July 29, 2026 - As we can see, the replaceAll() method replaces every character in input with “:“. This is because “.” has a special meaning in regex: matching any character. Next, let’s see how to tell the regex engine to treat special characters as literal.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-replaceall-method-explained-ec87fae6d15e
Medium
August 22, 2024 - Learn how to use Java's replaceAll() method with regular expressions for complex string manipulations like formatting input, sanitizing data, and more.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-replaceall-method
Java String replaceAll() Method - GeeksforGeeks
December 23, 2024 - String replaceAll method in Java searches for a specified string or regex pattern and returns a new string with the matched characters replaced.
🌐
Coderanch
coderanch.com › t › 511551 › java › string-replaceall-java-util-regex
string.replaceall() and java.util.regex.Pattern/Matcher (Beginning Java forum at Coderanch)
Beginning Java · joe nesbitt · Greenhorn · Posts: 17 · posted 15 years ago · Number of slices to send: Optional 'thank-you' note: Send · Hi all, i have a regex that checks if the content between the 5th and 6th occurence of | is "" as follows: (TEST\\|[||]*\\|\\|[||]*\\|[||]*\\|)\"\"") It works fine with string.replaceall().
🌐
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 ...
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.replaceall
String.ReplaceAll(String, String) Method (Java.Lang) | Microsoft Learn
An invocation of this method of the form str.replaceAll(regex,repl) yields exactly the same result as the expression · <blockquote> {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>) </blockquote> 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 java.util.regex.Matcher#replaceAll Matcher.replaceAll.
🌐
Coderanch
coderanch.com › t › 444665 › java › replace-character-replaceAll
How to replace character *, using .replaceAll (Beginning Java forum at Coderanch)
May 8, 2009 - This works: String oldStr = "XYZ*"; String newStr = oldStr.replaceAll("\\*", "[A-Za-z0-9]*"); System.out.println("newStr=" + newStr + "\n"); ... Welcome to JavaRanch You need to use \\ instead of \. The reason is that the String literal turns the \\ into a single \ before it analyses the regular ...
🌐
Medium
medium.com › javarevisited › micro-optimizations-in-java-string-replaceall-c6d0edf2ef6
Micro optimizations in Java. String.replaceAll | by Dmytro Dumanskiy | Javarevisited | Medium
September 8, 2020 - public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this) .replaceAll(replacement); }
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.regex.matcher.replaceall
Matcher.ReplaceAll Method (Java.Util.Regex) | Microsoft Learn
Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.
🌐
GitHub
github.com › openrewrite › rewrite › issues › 1659
Replace `String.replaceAll` with a compiled `Regular expression` · Issue #1659 · openrewrite/rewrite
April 16, 2022 - public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } class A { public void replace(){ String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!"; String changed = init.replaceAll("/[@]/g,", "_"); } } class A { private static final java.util.regex.Pattern p = Pattern.compile("/[@]/g,"); // compile it only once and reuse it public void replace(){ String init = "Bob is a Bird...
Author: openrewrite
🌐
Scaler
scaler.com › home › topics › java string replaceall()
Java String replaceAll() method
August 3, 2023 - The Java String class replaceAll() method is used to replace each substring that matches the regex of the string with the specified text, another string passed as a parameter.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - Parameters: regex - the regular ... · public String replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement....
🌐
TutorialsPoint
tutorialspoint.com › groovy › groovy_replaceall.htm
Groovy - String replaceAll() method
class Example { static void main(String[] args) { String str1 = "!!Tutorials!!Point", str2; String substr = "**", regex = "!!"; // prints string1 println("String = " + str1); /* replaces each substring of this string that matches the given regular expression with the given replacement */ str2 = str1.replaceAll(regex, substr); println("After Replacing = " + str2); } } When we run the above program, we will get the following result − ·
🌐
Manuals+
manuals.plus › file viewers › online tools › java tools › java string.replaceall(regex, repl) online
Java Regex Replace Online Tool | manuals.plus
April 18, 2026 - Object output = regexReplace(input, "/\\d+/", "#"); // Apply String.replaceAll(regex, repl) to the input value. System.out.println(output); // Print transformed output. Sample Input · abc123def456 · Sample Output · abc#def# Run the Java Regex Replace tool online with live input and output.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-replace-replacefirst-replaceall-method-examples
Java String replace(), replaceFirst() and replaceAll() methods
September 24, 2015 - PatternSyntaxException if the specified regular expression(regex) is not valid. String replaceAll(String regex, String replacement): It replaces all the substrings that fits the given regular expression with the replacement String.
🌐
Baeldung
baeldung.com › home › java › java string › remove or replace part of a string in java
Remove or Replace Part of a String in Java| Baeldung
June 15, 2024 - As their name implies, replaceAll() will replace every matched occurrence, while the replaceFirst() will replace the first matched occurrence: String master2 = "Welcome to Baeldung, Hello World Baeldung"; String regexTarget = "(Baeldung)$"; ...
🌐
Tutorial Gateway
tutorialgateway.org › java-string-replaceall-method
Java String replaceAll
May 23, 2016 - Java replaceAll replaces each substring that matches the regular expression with a new string. Let's show to write String.replaceAll in Java.
🌐
W3Schools
w3schools.com › java › java_variables_multiple.asp
Java Declare Multiple Variables
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods · charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods