Browse - Programming Tips - How can I do a string replace with a compiled pattern?

Date: 2012mar8
Language: Java

Q.  How can I do a string replace with a compiled pattern?

A.
static final 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.