How do I create a string search and replace using regex?
Category: java.util.regex, viewed: 13551 time(s).
In this example you'll see how we can create a small search and replace program using the regular expression classes in Java. The code below will replace all the brown word and change the color to red.
package org.kodejava.example.regex;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class StringReplace {
public static void main(String[] args) {
String source = "The quick brown fox jumps over the brown lazy dog.";
String find = "brown";
String replace = "red";
//
// Compiles the given regular expression into a pattern
//
Pattern pattern = Pattern.compile(find);
//
// Creates a matcher that will match the given input against the pattern
//
Matcher matcher = pattern.matcher(source);
//
// Replaces every subsequence of the input sequence that matches the
// pattern with the given replacement string
//
String output = matcher.replaceAll(replace);
System.out.println("Source = " + source);
System.out.println("Output = " + output);
}
}
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!