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 OverflowW3Schools
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"));
Top answer 1 of 4
7
Try this:
String input = "Hello cat Cats cats Dog dogs dog fox foxs Foxs";
input = input.replaceAll("(?i)\\s*(?:fox|dog|cat)s?", "");
Demo
2 of 4
3
Maybe you can try to match everything except the word Hello.
Something like:
string.replaceAll("(?!Hello)\\b\\S+", "");
You can test it in this link.
The idea is to perform a negative lookahead for Hello word, and get any other word present.
03:27
Java String replaceAll() method | How to use replaceAll in String ...
01:37
Java String: Pattern Replacement with replaceAll(String regex, ...
How to use replaceAll and replaceFirst methods of matcher ...
04:02
Difference between Replace and replaceAll in Java - YouTube
Java - Lesson 15.6 (Replacing Part of a String Using Regular ...
01:26
Understanding Why String.replaceAll Doesn't Work with $ Symbols ...
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 ...
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 ...
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.
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