Dave's Brain

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.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.