Mega Code Archive

 
Categories / Java / Regular Expressions
 

Create a string search and replace using regex

import java.util.regex.Pattern; import java.util.regex.Matcher; public class Main {   public static void main(String[] args) {     String source = "The quick brown fox jumps over the brown lazy dog.";     String find = "brown";     String replace = "red";     Pattern pattern = Pattern.compile(find);     Matcher matcher = pattern.matcher(source);     String output = matcher.replaceAll(replace);     System.out.println("Source = " + source);     System.out.println("Output = " + output);   } }