How to replace all occurrences by regex?

Member

by lottie , in category: Third Party Scripts , 4 months ago

How to replace all occurrences by regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 4 months ago

@lottie 

You can use the replaceAll() method in Java to replace all occurrences of a pattern using regex. Here's an example:

1
2
3
4
5
6
7
String input = "Hello, World! Hello, Universe!";
String pattern = "Hello";
String replacement = "Hi";

String output = input.replaceAll(pattern, replacement);

System.out.println(output);


Output:

1
Hi, World! Hi, Universe!


In this example, we are replacing all occurrences of the word "Hello" with "Hi" in the input string. You can customize the regex pattern to match any specific pattern or expression that you want to replace.

Related Threads:

How to replace a substring with a regex pattern using regex in python?
How to replace string by regex?
How to replace part of a url with regex?
How to replace a specific character in regex?
How to replace string before string using regex?
How to replace the string using regex in javascript?