Programming Tips - Java: Do a string replace with a compiled pattern

Date: 2012mar8 Language: Java Q. Java: Do a string replace with a compiled pattern A.
Pattern myPattern = Pattern.compile("regex", Pattern.CASE_INSENSITIVE); String exampleCompiledPatternReplace(String in, String changeTo) { Matcher dmatcher = myPattern.matcher(in); return dmatcher.replaceFirst(changeTo); }
You can use String's replaceFirst() or replaceAll() if don't want to compile the pattern.